jmespath / jmespath.py

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

Cannot get filter to work #199

Closed Kollibri closed 4 years ago

Kollibri commented 4 years ago

I have the following json:

{ "ip_pool": [ { "ip": "10.231.41.75", "nat": "10.231.41.75", "available": "false" }, { "ip": "10.231.41.76", "nat": "10.231.41.76", "available": "true" }, { "ip": "10.231.41.77", "nat": "10.231.41.77", "available": "true" }, { "ip": "10.231.41.78", "nat": "10.231.41.78", "available": "true" } ] }

And I am trying to use the following jmespath query to return all results where "available" is true.

ip_pool[?available=="true"]

But this returns an empty array. From what I can see, this should be pretty simple, I'm not sure what I'm missing.

samukweku commented 4 years ago

i figured it out, even though I cant explain it, you've got to wrap the true in double quotes, and then in back ticks. hopefully searching on google can give the reason why :

expression = jmespath.compile('ip_pool[?available==` "true" `]')

jamesls commented 4 years ago

The double quotes are used to specify a "quoted identifier" (https://jmespath.org/specification.html#identifiers) which is essentially a field, similiar to how ip_pool in your query is an unquoted identifier. So essentially your original query is the same thing as saying ip_pool[?available==true].

If you want to specify the literal string true instead of a field named true, you can either use the syntax you used with the backticks, or as a shorthand, you can use the single quotes:

ip_pool[?available=='true']

Hope that helps, glad you were able to figure it out!