0

Basically I want to filter a nested array of that shape:

[
  [0, {…}],
  [1, {…}],
  …
]

this jms path expression is working on the playground:

[?@[0] > `0`]

and when run like that from an ansible playbook:

- debug: msg="{{ [[1],[2],[3],[4]] | community.general.json_query('[?@[0] > `1`]') }}"

but when done like that:

- debug: msg="{{ range(0,3) | zip(range(3,0,-1)) | community.general.json_query('[?@[0] > `1`]') }}"

it yields an empty list ([])

Boiling it down even more:

ansible@r2d2:~$ ansible localhost -m debug -a $'msg="{{ range(0, 3) | zip(range(3,0,-1)) | json_query(\'@[0]\')  }}"'
localhost | SUCCESS => {
    "msg": "(0, 3)"
}

Show that the result is some sort of tuple, which is no json data type at all. The data that I would like to filter is constructed like the above, using range and zip so how can make the expression work in those circumstances?

UPDATE

Going deeper on that showed that this whole problem has two causes.

  1. {{ range(0,3) | type_debug }} => 'range' can easily be solved by {{ range(0,3) | list | … }}

  2. {{ [0] | list | zip([0]) | list | json_query('@[0]') | type_debug }} => 'tuple'

There seams to be a python solution here, but how to do that in jinja??

philipp
  • 15,947
  • 15
  • 61
  • 106

1 Answers1

0

Finally. This code is working. Thanks to

[…] | zip([…]) | map('list')

that turn all tuple items into lists which work with json_query (jmspath).

- debug: msg="{{ indexed | community.general.json_query(q) }}"
  vars:
    # using this way to write the query resolves a lot quoting issues
    q: >-
      [?@[0] > `0`]
    keys: "{{ range(0, 3) }}"
    # map('list') is the magic sauce here
    indexed: "{{ keys | zip([3,2,1]) | map('list') }}"
philipp
  • 15,947
  • 15
  • 61
  • 106