1

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

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

Does it mean that Ansible implementation doesnt allow wildcard search ? just confused why i got the same result with much shorter syntax in that online checker

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.