i have complex result in JSON format and struggling to extract values out of it. I tested the JSONpath online evaluator https://jsonpath.com/ where i can just simply type
$.[*].toPort
and get the list of requested variables.
I've tried to use the same syntax in Ansible but getting empty list.
- name: JSON test
hosts: localhost
gather_facts: no
vars:
jsoncontent:
{
"infraAccPortP": {
"attributes": {
"annotation": "",
"childAction": "",
"descr": "",
"nameAlias": "",
"ownerKey": "",
"ownerTag": "",
"status": "",
"uid": "15374"
},
"children": [
{
"infraHPortS": {
"attributes": {
"annotation": "",
"uid": "8927"
},
"children": [
{
"infraPortBlk": {
"attributes": {
"fromPort": "41",
"toPort": "41",
"uid": "8927"
}
}
}
]
}
},
{
"infraHPortS": {
"attributes": {
"annotation": "",
"uid": "8927"
},
"children": [
{
"infraPortBlk": {
"attributes": {
"fromPort": "42",
"toPort": "42",
"uid": "8927"
}
}
}
]
}
}
]
}
}
tasks:
- name: show jsoncontent
debug:
var: jsoncontent
- name: Show just toPort values
debug:
msg: "{{ jsoncontent | json_query(jmesquery) }}"
vars:
jmesquery: "[*].toPort"
Any help how to adjust the query to get the expected result? Its bit frustrating as there is not much documentation on how json_query is implemented in Ansible
The task below does the job
- name: Show just toPort values
debug:
msg: "{{ jsoncontent.infraAccPortP.children | json_query(jmesquery) }}"
vars:
jmesquery: "[].infraHPortS.children[].infraPortBlk.attributes.toPort"
gives
msg:
- '41'
- '42'
Q: "Ansible implementation doesn't allow wildcard searches?"
A: There doesn't seem to be anything special in the implementation. (This filter moved to ansible-collections/community.general since 2.10.)
Q: "Just confused why I got the same result with much shorter syntax in that online checker."
A: The question is how the checker is implemented, I think. jq
doesn't work this way either. For example
shell> cat data.json | jq .jsoncontent.infraAccPortP.children[].infraHPortS.children[].infraPortBlk.attributes.toPort
"41"
"42"
The "checker" query crashes
shell> cat data.json | jq .[*].toPort
jq: error: syntax error, unexpected '*' (Unix shell quoting issues?) at <top-level>, line 1:
.[*].toPort
jq: 1 compile error
, or gives an empty result
shell> cat data.json | jq .[].toPort
null