jmespath / jmespath.py

JMESPath is a query language for JSON.

Home Page:http://jmespath.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cannot get filter to work

Kollibri opened this issue · comments

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.

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" `]')

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!