kharandziuk
kharandziuk

Reputation: 12890

query with filter in JsonPath

I have a structure like this:

{
    title: [
      {lang: “en”, value: “snickers”},
      {lang: “ru”, value: “сниккерс”}
    ]
}

I need to take a result such this:

“snickers”

I finished with query: $.title[?(@.lang="en")].value

but it doesn't work

Upvotes: 1

Views: 287

Answers (1)

Duncan
Duncan

Reputation: 841

JsonPath always returns an array of results, so you will not be able to get a single value such as "snickers" on its own. You will be able to get ["snickers"] though. To get it working you need to use a double equal sign instead of single:

$.title[?(@.lang=="en")].value

Also note that another reason you may be having issues is that your double quotes in the json are not the standard double quote characters. Depending on how you consume the json you may also need to wrap the property names in double quotes. The json standard is quite strict, and if you are parsing your json from text it has to follow these rules. Here is version of the json corrected in this way:

{
    "title": [
      {"lang": "en", "value": "snickers"},
      {"lang": "ru", "value": "сниккерс"}
    ]
}

I have tested the above query with the corrected json using http://www.jsonquerytool.com.

Upvotes: 1

Related Questions