-
Notifications
You must be signed in to change notification settings - Fork 5
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
Let us select dict values by criteria on their associated keys #20
Comments
I like it. Having a built in function for this would be really useful. I've also seen requests for the opposite case: going from a list of key/val mappings back to a hash, so having a pair of functions that could convert to/from both forms would be really helpful. |
I'm also interested in this. How about the following set of functions:
I implemented these for myself using the import jmespath
from jmespath import functions
from jmespath.compat import iteritems
from jmespath.compat import map
class CustomFunctions(functions.Functions):
@functions.signature({'types': ['object']})
def _func_items(self, arg):
return list(map(list, iteritems(arg)))
@functions.signature({'types': ['array'], 'variadic': True})
def _func_zip(self, *arguments):
return list(map(list, zip(*arguments)))
@functions.signature({'types': ['array']})
def _func_to_object(self, pairs):
return dict(pairs)
subject = {
'keys': [
u'One',
u'Two',
u'Three',
u'Four'
],
'values': [
1,
2,
3,
4
]
}
options = jmespath.Options(custom_functions=CustomFunctions())
# prints {u'Four': 4, u'Three': 3, u'Two': 2, u'One': 1}
mapped = jmespath.search('to_object(zip(keys, values))', subject, options=options)
print(mapped)
# prints {u'Three': 3, u'Two': 2}
print(
jmespath.search(
'items(@) | [?starts_with([0], `T`)] | to_object(@)', mapped, options=options)
) I would be happy to add tests for these and submit a PR for the python implementation. |
I like it. I think this is a good balance between ease of use/familiarity as well as exposing useful low level primitives that you can build on. 👍 from me. @jmespath/dev any concerns? |
👍 from me |
👍 from me too. |
Renamed |
4 years old issue.. why taking so much? |
Friendly ping! |
Let's say I have
and want to select all values for which keys start with "foo". I tried for a bit to get JMESPath to get me
['foo', 'woof', 'meow']
but was unsuccessful. I don't know if it's impossible, but I was thinking that a simple approach might be a built-in function calledpivot
/reify
/mappings
(I'm bad at naming) that transforms the above into a list of Key/Value mappings, so:which seems far easier to deal with. I'm open to other approaches to achieving the same goal, of course 😄
The text was updated successfully, but these errors were encountered: