jsonpath eval not working
Closed this issue · 11 comments
I am trying to get name of of all states from json where 'hasAgency' is true
I tried JsonPath.new("$..name[?(@['hasAgency'] == true)]").on(json_response)
and it returns empty array
I also tried JsonPath.new("$..name[?(@['hasAgency'] != false)]").on(json_response)
and it returns error "undefined method `strip' for nil:NilClass"
I tried jsonpath $[?(@['hasAgency'] != false)].name
at http://jsonpath.com/ and it work so I vene tried that path but it is yielding the same result as well.
Here's the JSON:
[ { "name": "Alabama", "abbrev": "AL", "hasAgency": true }, { "name": "Alaska", "abbrev": "AK", "hasAgency": false }, { "name": "Arizona", "abbrev": "AZ", "hasAgency": true }, { "name": "Arkansas", "abbrev": "AR", "hasAgency": true }, { "name": "California", "abbrev": "CA", "hasAgency": true }, { "name": "Colorado", "abbrev": "CO", "hasAgency": true }, { "name": "Connecticut", "abbrev": "CT", "hasAgency": true }, { "name": "Delaware", "abbrev": "DE", "hasAgency": true }, { "name": "Florida", "abbrev": "FL", "hasAgency": true }, { "name": "Georgia", "abbrev": "GA", "hasAgency": true }, { "name": "Hawaii", "abbrev": "HI", "hasAgency": true }, { "name": "Idaho", "abbrev": "ID", "hasAgency": true }, { "name": "Illinois", "abbrev": "IL", "hasAgency": true }, { "name": "Indiana", "abbrev": "IN", "hasAgency": true }, { "name": "Iowa", "abbrev": "IA", "hasAgency": true }, { "name": "Kansas", "abbrev": "KS", "hasAgency": true }, { "name": "Kentucky", "abbrev": "KY", "hasAgency": true }, { "name": "Louisiana", "abbrev": "LA", "hasAgency": true }, { "name": "Maine", "abbrev": "ME", "hasAgency": true }, { "name": "Maryland", "abbrev": "MD", "hasAgency": true }, { "name": "Massachusetts", "abbrev": "MA", "hasAgency": true }, { "name": "Michigan", "abbrev": "MI", "hasAgency": true }, { "name": "Minnesota", "abbrev": "MN", "hasAgency": true }, { "name": "Mississippi", "abbrev": "MS", "hasAgency": true }, { "name": "Missouri", "abbrev": "MO", "hasAgency": true }, { "name": "Montana", "abbrev": "MT", "hasAgency": true }, { "name": "Nebraska", "abbrev": "NE", "hasAgency": true }, { "name": "Nevada", "abbrev": "NV", "hasAgency": true }, { "name": "New Hampshire", "abbrev": "NH", "hasAgency": true }, { "name": "New Jersey", "abbrev": "NJ", "hasAgency": true }, { "name": "New Mexico", "abbrev": "NM", "hasAgency": true }, { "name": "New York", "abbrev": "NY", "hasAgency": true }, { "name": "North Carolina", "abbrev": "NC", "hasAgency": true }, { "name": "North Dakota", "abbrev": "ND", "hasAgency": true }, { "name": "Ohio", "abbrev": "OH", "hasAgency": true }, { "name": "Oklahoma", "abbrev": "OK", "hasAgency": true }, { "name": "Oregon", "abbrev": "OR", "hasAgency": true }, { "name": "Pennsylvania", "abbrev": "PA", "hasAgency": true }, { "name": "Puerto Rico", "abbrev": "PR", "hasAgency": true }, { "name": "Rhode Island", "abbrev": "RI", "hasAgency": true }, { "name": "South Carolina", "abbrev": "SC", "hasAgency": true }, { "name": "South Dakota", "abbrev": "SD", "hasAgency": true }, { "name": "Tennessee", "abbrev": "TN", "hasAgency": true }, { "name": "Texas", "abbrev": "TX", "hasAgency": true }, { "name": "Utah", "abbrev": "UT", "hasAgency": true }, { "name": "Vermont", "abbrev": "VT", "hasAgency": true }, { "name": "Virginia", "abbrev": "VA", "hasAgency": true }, { "name": "Washington", "abbrev": "WA", "hasAgency": true }, { "name": "West Virginia", "abbrev": "WV", "hasAgency": true }, { "name": "Wisconsin", "abbrev": "WI", "hasAgency": true }, { "name": "Wyoming", "abbrev": "WY", "hasAgency": false } ]
Hi.
Eval has been removed from JsonPath as it's deemed unsafe. That said, I don't understand your Json. :) It's mixing types where string can be a boolean as well, which is not particularly something JsonPath will or should understand.
Ups, never mind. I misread it. Let me take a look again.
[
{
"name":"Alabama",
"abbrev":"AL",
"hasAgency":true
},
{
"name":"Alaska",
"abbrev":"AK",
"hasAgency":false
},
...
]
Oh, I see. name
will not have an attribute called hasAgency
. If you start out with ..name
it will look for something like:
{
"name": [
{
"hasAgency": true,
}
]
}
Which if you look at your Json it will obviously not found.
For your case what would work is this jsonpath: $..[?(@.hasAgency == true)]
.
@Skarlso - thanks for the prompt response. I guess I have already tried $..[?(@.hasAgency == true)]
but I am not sure so I will try it out again and let you know if it works.
The json is response from an API that I am trying to test.
@sandeepnagra sure no problem. Tell me if it's not working. I'll investigate further.
@Skarlso - I tried JsonPath.new("$..[?(@.hasAgency == true)]").on(json_response)
but it didn't work. It just returns an empty array. Eventually, I just want only the names of the states that has hasAgency == true, so I would want something like $..[?(@.hasAgency == true)].name
to work.
@sandeepnagra Interesting. Thanks, I'll investigate further. Must be something wrong with the Parser.
@sandeepnagra So, I played around with it. Then suddenly realized, you can do simply $..[?(@.hasAgency)]
and it works. :) Would you please be so kind as to test it out? Thanks.
@Skarlso - $.[?(@.hasAgency)].name
did the trick for what I wanted. This was helpful. Thanks!
Awesome! Glad to hear. :-) Cheers.