# Need help for json query syntax

**URL:** <https://forum.ansible.com/t/need-help-for-json-query-syntax/34381>\
**Category:** Ansible Project\
**Created:** [December 30, 2020, 2:46pm UTC](https://forum.ansible.com/t/need-help-for-json-query-syntax/34381 "2020-12-30T14:46:37Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ramesh\_AR](https://avatars.discourse-cdn.com/v4/letter/r/3e96dc/32.png) [@Ramesh\_AR](https://forum.ansible.com/u/Ramesh_AR)\
**Post date:** [December 30, 2020, 2:46pm UTC](https://forum.ansible.com/t/need-help-for-json-query-syntax/34381/1 "2020-12-30T14:46:37Z")

</div>

Folks, trying to get json query for the below data’s.

Tried the below queries but didn’t get output as expected. can someone help me .  
Try 1:

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [December 30, 2020, 3:28pm UTC](https://forum.ansible.com/t/need-help-for-json-query-syntax/34381/2 "2020-12-30T15:28:53Z")

</div>

Post the (link to) "result". For example

&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: result.json

You might want to paste it  
[https://alternativeto.net/software/pastebin/](https://alternativeto.net/software/pastebin/)

---

<div class="post-metadata">

**Author:** ![Ramesh\_AR](https://avatars.discourse-cdn.com/v4/letter/r/3e96dc/32.png) [@Ramesh\_AR](https://forum.ansible.com/u/Ramesh_AR)\
**Post date:** [December 30, 2020, 4:21pm UTC](https://forum.ansible.com/t/need-help-for-json-query-syntax/34381/3 "2020-12-30T16:21:12Z")

</div>

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [Host1] =\> {  
“result”: {  
“aaaLogin”: {  
“attributes”: {  
“cookie”: “”,  
“outCookie”: “1609345308/14c4aad7-b7b1-17b1-a10e-bc16ee6a0f70”,  
“outPriv”: “admin”,  
“outRefreshPeriod”: “600”,  
“outSessionId”: “8470”,  
“outVersion”: “3.1(3a)”,  
“response”: “yes”  
}  
},  
“ansible\_facts”: {  
“discovered\_interpreter\_python”: “/usr/bin/python3”  
},  
“changed”: false,  
“configResolveClass”: {  
“attributes”: {  
“classId”: “equipmentPsu”,  
“cookie”: “1609345308/14c4aad7-b7b1-17b1-a10e-bc16ee6a0f70”,  
“response”: “yes”  
},  
“children”: [  
{  
“outConfigs”: {  
“attributes”: {},  
“children”: [  
{  
“equipmentPsu”: {  
“attributes”: {  
“dn”: “sys/rack-unit-1/psu-1”,  
“fwVersion”: “12002216”,  
“id”: “1”,  
“input”: “138”,  
“maxOutput”: “NA”,  
“model”: “700-014160-0000”,  
“operability”: “operable”,  
“output”: “112”,  
“pid”: “UCSC-PSU1-770W”,  
“power”: “on”,  
“presence”: “equipped”,  
“serial”: “ART2221F58Q”,  
“thermal”: “ok”,  
“vendor”: “Cisco Systems Inc”,  
“voltage”: “ok”  
}  
}  
},  
{  
“equipmentPsu”: {  
“attributes”: {  
“dn”: “sys/rack-unit-1/psu-2”,  
“fwVersion”: “12002216”,  
“id”: “2”,  
“input”: “145”,  
“maxOutput”: “NA”,  
“model”: “700-014160-0000”,  
“operability”: “operable”,  
“output”: “118”,  
“pid”: “UCSC-PSU1-770W”,  
“power”: “on”,  
“presence”: “equipped”,  
“serial”: “ART2221F4XS”,  
“thermal”: “ok”,  
“vendor”: “Cisco Systems Inc”,  
“voltage”: “ok”  
}  
}  
}  
]  
}  
}  
]  
},  
“elapsed”: 3,  
“failed”: false,  
“response”: “OK (unknown bytes)”,  
“status”: 200  
}  
}

PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
host1 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [December 30, 2020, 10:55pm UTC](https://forum.ansible.com/t/need-help-for-json-query-syntax/34381/4 "2020-12-30T22:55:44Z")

</div>

> Folks, trying to get json query for the below data’s.
> 
> Tried the below queries but didn’t get output as expected. can someone help me .

Apologies for the obvious question, but what output do you expect?

---

<div class="post-metadata">

**Author:** ![Ramesh\_AR](https://avatars.discourse-cdn.com/v4/letter/r/3e96dc/32.png) [@Ramesh\_AR](https://forum.ansible.com/u/Ramesh_AR)\
**Post date:** [December 30, 2020, 11:11pm UTC](https://forum.ansible.com/t/need-help-for-json-query-syntax/34381/5 "2020-12-30T23:11:00Z")

</div>

Hello Dick,

I’m trying to get like the below format and also its listing I wanted to join (’ , ') “dn”: “sys/rack-unit-1/psu-1” , “id”: “1”

“msg”: The PSU output is, {  
“dn”: “sys/rack-unit-1/psu-1”,  
“id”: “1”,  
“operability”: “operable”,  
“presence”: “equipped”

“dn”: “sys/rack-unit-1/psu-2”,  
“id”: “2”,  
“operability”: “operable”,  
“presence”: “equipped”

}  
}

Thanks,  
Ramesh

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [December 31, 2020, 2:20am UTC](https://forum.ansible.com/t/need-help-for-json-query-syntax/34381/6 "2020-12-31T02:20:45Z")

</div>

Try this  
[https://gist.github.com/vbotka/7ed043e08550e83a929593e3ebd510b9](https://gist.github.com/vbotka/7ed043e08550e83a929593e3ebd510b9)

This is the same

&nbsp;&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PSU: "{{ result.configResolveClass.children|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;json\_query(my\_query1)|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;json\_query(my\_query2)|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;json\_query(my\_query3) }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my\_query1: ".outConfigs.children"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my\_query2: ".equipmentPsu.attributes"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my\_query3: ".{dn: dn,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;id: id,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;operability: operability,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;presence: presence}"

---

<div class="post-metadata">

**Author:** ![Ramesh\_AR](https://avatars.discourse-cdn.com/v4/letter/r/3e96dc/32.png) [@Ramesh\_AR](https://forum.ansible.com/u/Ramesh_AR)\
**Post date:** [December 31, 2020, 3:23pm UTC](https://forum.ansible.com/t/need-help-for-json-query-syntax/34381/7 "2020-12-31T15:23:27Z")

</div>

Hello Vladimir,

Thanks for the help. however, I set fact on my playbook and doesn’t show any output.

- set\_fact:  
PSU: “{{ result.configResolveClass.children|json\_query(my\_query1)|json\_query(my\_query2)|json\_query(my\_query3) }}”  
vars:  
my\_query1: “.outConfigs.children”  
my\_query2: “.equipmentPsu.attributes”  
my\_query3: “.{dn: dn, d: id,operability: operability,presence: presence}”

PLAY [mdhd] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

TASK [Checking PSU status] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [host1]

TASK [set\_fact] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [host1]

PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
host1 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

Thanks,  
Ramesh

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [December 31, 2020, 3:41pm UTC](https://forum.ansible.com/t/need-help-for-json-query-syntax/34381/8 "2020-12-31T15:41:57Z")

</div>

This is strange. I added the file with the data. Both versions of the  
code work for me

&nbsp;&nbsp;PSU:  
&nbsp;&nbsp;- dn: sys/rack-unit-1/psu-1  
&nbsp;&nbsp;&nbsp;&nbsp;id: '1'  
&nbsp;&nbsp;&nbsp;&nbsp;operability: operable  
&nbsp;&nbsp;&nbsp;&nbsp;presence: equipped  
&nbsp;&nbsp;- dn: sys/rack-unit-1/psu-2  
&nbsp;&nbsp;&nbsp;&nbsp;id: '2'  
&nbsp;&nbsp;&nbsp;&nbsp;operability: operable  
&nbsp;&nbsp;&nbsp;&nbsp;presence: equipped

---

<div class="post-metadata">

**Author:** ![Ramesh\_AR](https://avatars.discourse-cdn.com/v4/letter/r/3e96dc/32.png) [@Ramesh\_AR](https://forum.ansible.com/u/Ramesh_AR)\
**Post date:** [December 31, 2020, 3:48pm UTC](https://forum.ansible.com/t/need-help-for-json-query-syntax/34381/9 "2020-12-31T15:48:03Z")

</div>

Ok. here’s my playbook.

when I use -vvv option, I see those outputs.

---

<div class="post-metadata">

**Author:** ![Ramesh\_AR](https://avatars.discourse-cdn.com/v4/letter/r/3e96dc/32.png) [@Ramesh\_AR](https://forum.ansible.com/u/Ramesh_AR)\
**Post date:** [December 31, 2020, 4:00pm UTC](https://forum.ansible.com/t/need-help-for-json-query-syntax/34381/10 "2020-12-31T16:00:15Z")

</div>

Ok got it . I figured out …

just added in mypalybook

- debug: var=PSU

PLAY [mdhd] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

TASK [Checking PSU status] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [host1]

TASK [set\_fact] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [host1]

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [host1] =\> {  
“PSU”: [  
{  
“dn”: “sys/rack-unit-1/psu-1”,  
“id”: “1”,  
“operability”: “operable”,  
“presence”: “equipped”  
},  
{  
“dn”: “sys/rack-unit-1/psu-2”,  
“id”: “2”,  
“operability”: “operable”,  
“presence”: “equipped”  
}  
]  
}

PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
host1 : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
