jmespath.py icon indicating copy to clipboard operation
jmespath.py copied to clipboard

treat tuples as lists

Open Inilien opened this issue 6 years ago • 2 comments

import jmespath

a_work = [[{"a": 1}, {"b": 2}]]

a_not_work = list(zip([{"a": 1}], [{"b": 2}]))  # jmespath cannot be applied to zipped result
assert a_not_work == [({"a": 1}, {"b": 2})]  # since zipped result creates tuples instead of arayes

jpath = '[*].{s: [0], t: [1]}'

res_work = jmespath.search(jpath, a_work)
res_not_work = jmespath.search(jpath, a_not_work)

print("works as expected\n", f"input: {a_work}, output: {res_work}")
print("doesn't work as expected\n", f"input: {a_not_work}, output: {res_not_work}")

the code above will print the following results:

works as expected
 input: [[{'a': 1}, {'b': 2}]], output: [{'s': {'a': 1}, 't': {'b': 2}}]
doesn't work as expected
 input: [({'a': 1}, {'b': 2})], output: [{'s': None, 't': None}]

It would be great if tuples were treated as lists. It would allow to apply jmespath dirrectly to list(zip(..)) results.

Inilien avatar Mar 10 '19 13:03 Inilien

It's not just functions, selectors like @[0] don't work either.

>>> data = [ ('foo', 'one'), ('bar', 'two'), ]
>>> jmespath.search('[*].[@[1], @[0]]', data)
[[None, None], [None, None]]   # broken

I expect to instead get:

>>> jmespath.search('[*].[@[1], @[0]]', data)
[['one', 'foo'], ['two', 'bar']]

dekimsey avatar Jun 28 '20 21:06 dekimsey

jmespath is used in Ansible json_query.

Queries in data produced with many functions contain tuples. For example product, which is the same function as in itertools contains tuples, and regex_findall i.e. re.findall.

It is quite a surprise to find the jmespath query does not work and it takes a lot of time to figure out to convert all tuples into lists (with map('list') filter or such).

kohtala avatar Nov 12 '21 09:11 kohtala