jmespath / jmespath.py

JMESPath is a query language for JSON.
http://jmespath.org
MIT License
2.19k stars 181 forks source link

Issue with parsing json based on string condition with variable key as part of the path #184

Closed rams3sh closed 5 years ago

rams3sh commented 5 years ago

I am working on a json with structure that looks like this

{
  "people": {
    "a": {"First":"James", "last": "1"},
    "b": {"First":"Jacob", "last": "2"},
    "c": {"First":"Jayden", "last": "3"},
    "d": {"First":"different", "last" : "4"}
  }
}

I am trying to get the the last values of those with first value starting with "J".

There are variable keys in between i.e a,b,c etc ..

I have tried it using jq and the query looks something like this

'.people | .[] | select (.First | startswith("J")) | .last'

However , I am unable to achieve the same with jmespath. The closest that I could get logically based on my understanding from documentations available online was

'people.*[?starts_with(arn,`J`)].last'

However , it yields an empty value.

rams3sh commented 5 years ago

The solution is to use values(@).

Reference link https://github.com/jmespath/jmespath.site/issues/24

So one of the possible solution for the above ask is

people.values(@)[?starts_with(First,`J`)].last

Would be nice if such examples of wildcard key based condition filters are added to jmespath tutorials. Most of the ones there currently focus on indexes with standard key structure.

Cheers

Raimond56 commented 2 years ago

This was a big help! Thanks! Did not manage to find these wildcard examples anywhere else