# mapping json variable to list using set\_fact

**URL:** <https://forum.ansible.com/t/mapping-json-variable-to-list-using-set-fact/31454>\
**Category:** Ansible Developer\
**Created:** [October 11, 2019, 7:09am UTC](https://forum.ansible.com/t/mapping-json-variable-to-list-using-set-fact/31454 "2019-10-11T07:09:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Balasubramaniam\_M](https://avatars.discourse-cdn.com/v4/letter/b/8baadc/32.png) [@Balasubramaniam\_M](https://forum.ansible.com/u/Balasubramaniam_M)\
**Post date:** [October 11, 2019, 7:09am UTC](https://forum.ansible.com/t/mapping-json-variable-to-list-using-set-fact/31454/1 "2019-10-11T07:09:09Z")

</div>

Hello All,

I have register variable with below json as value and need to extract name and dn from this JSON to list. tried different options using set\_fact without any luck. Could you please someone help? this is really urgent

```auto
{
    "nodes": {
        "status": -1,
        "imdata": [
            {
                "fabricNode": {
                    "attributes": {
                        "status": "",
                        "dn": "topology/pod-1/node-1",
                        "name": "NOQCJALAB1"
                    }
                }
            },
            {
                "fabricNode": {
                    "attributes": {
                        "status": "",
                        "dn": "topology/pod-1/node-1",
                        "name": "NOQCJALAB2"
                    }
                }
            }
        ],
        "totalCount": 2,
        "changed": false,
        "failed": false
    },
    "changed": false,
    "_ansible_verbose_always": true,
    "_ansible_no_log": false
}

```

---

<div class="post-metadata">

**Author:** ![Chris\_Church1](https://avatars.discourse-cdn.com/v4/letter/c/ac8455/32.png) [@Chris\_Church1](https://forum.ansible.com/u/Chris_Church1)\
**Post date:** [October 11, 2019, 2:05pm UTC](https://forum.ansible.com/t/mapping-json-variable-to-list-using-set-fact/31454/2 "2019-10-11T14:05:02Z")

</div>

The json\_query filter is probably what you’re looking for:  
[https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_filters.html#json-query-filter](https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html#json-query-filter)

```auto
- set_fact:
    new_var: "{{ your_var | json_query('nodes.imdata[*].fabricNode.attributes.{dn:dn,name:name}') }}"

```

should give you:

```auto
[
  {
    "dn": "topology/pod-1/node-1",
    "name": "NOQCJALAB1"
  },
  {
    "dn": "topology/pod-1/node-1",
    "name": "NOQCJALAB2"
  }
]

```

---

<div class="post-metadata">

**Author:** ![Balasubramaniam\_M](https://avatars.discourse-cdn.com/v4/letter/b/8baadc/32.png) [@Balasubramaniam\_M](https://forum.ansible.com/u/Balasubramaniam_M)\
**Post date:** [October 11, 2019, 3:13pm UTC](https://forum.ansible.com/t/mapping-json-variable-to-list-using-set-fact/31454/3 "2019-10-11T15:13:55Z")

</div>

Many thanks for the response:

I have one more array within imdata. COuld you please guide me how to extract fields? it is somewhat like

```auto
 new_var: "{{ your_var | json_query('nodes.imdata[*].result[*].fabricNode.attributes.{dn:dn,name:name}') }}"

```

also in could you please tell me mention that? just variable or upto array

I appreciate your help

thanks
