Need help for json query syntax

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:

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

    - debug:
        var: result.json

You might want to paste it
https://alternativeto.net/software/pastebin/

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

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?

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

Try this
https://gist.github.com/vbotka/7ed043e08550e83a929593e3ebd510b9

This is the same

    - 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,
                        id: id,
                        operability: operability,
                        presence: presence}"

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

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

  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

Ok. here’s my playbook.

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

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