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

Let us select dict values by criteria on their associated keys #20

Open
copumpkin opened this issue Nov 5, 2015 · 8 comments · May be fixed by jmespath/jmespath.site#21
Open

Let us select dict values by criteria on their associated keys #20

copumpkin opened this issue Nov 5, 2015 · 8 comments · May be fixed by jmespath/jmespath.site#21
Labels
enhancement New feature or request

Comments

@copumpkin
Copy link

Let's say I have

{
  'bark': 'oink',
  'foo1': 'foo',
  'foo2': 'woof',
  'foo3': 'meow'
}

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 called pivot/reify/mappings (I'm bad at naming) that transforms the above into a list of Key/Value mappings, so:

[
  { 'Key': 'bark', 'Value': 'oink' },
  { 'Key': 'foo1', 'Value': 'foo' },
  { 'Key': 'foo2', 'Value': 'woof' },
  { 'Key': 'foo3', 'Value': 'meow' }
]

which seems far easier to deal with. I'm open to other approaches to achieving the same goal, of course 😄

@jamesls
Copy link
Member

jamesls commented Nov 5, 2015

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.

@jamesls jamesls added the enhancement New feature or request label Nov 5, 2015
@jstewmon
Copy link

I'm also interested in this. How about the following set of functions:

  • items array[array[string|number|boolean|null]] items(object)
  • zip array[array[*]] zip([array *argument, [, array $...]])
  • to_object object to_object(array[array[string|number|boolean|null]])

I implemented these for myself using the CustomFunctions class in the python implementation (assumes map and iteritems are added to jmespath.compat):

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.

@jamesls
Copy link
Member

jamesls commented Mar 18, 2016

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?

@mtdowling
Copy link

👍 from me

@velavokr
Copy link

👍 from me too.

@jstewmon
Copy link

Renamed to_object to from_items based on the discussion on jmespath/jmespath.py#105

@rodfersou
Copy link

4 years old issue.. why taking so much?

@EdSchouten
Copy link

Friendly ping!

@jamesls jamesls transferred this issue from jmespath/jmespath.site Mar 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants