Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

treat tuples as lists #179

Open
Inilien opened this issue Mar 10, 2019 · 2 comments
Open

treat tuples as lists #179

Inilien opened this issue Mar 10, 2019 · 2 comments

Comments

@Inilien
Copy link

Inilien commented Mar 10, 2019

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.

@dekimsey
Copy link

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']]

@kohtala
Copy link

kohtala commented Nov 12, 2021

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants