Reputation: 23
On the below JSON example, I need to get only 1 ID when 'contenttype'!='helptext'
I cannot do 'contenttype'=='textAreaSelectionLong'
because a lot of other types are possible.
Since it's on a different node on the same level as id, I don't know how to write it in a single JSON path expression.
I tried the below JSON path,
$.children[?(@.name=='p')].children[?(@.name=='@id')].text
And it gives me,
[
"id2",
"id3"
]
But I am expecting a JSON path that could return only id2
Below is a chunk of JSON that this example is based on,
{
"children": [
{
"name": "@id",
"text": "id1",
"children": []
},
{
"name": "contenttype",
"text": "question",
"children": []
},
{
"name": "p",
"text": "Some random text 1",
"children": [
{
"name": "contenttype",
"text": "textAreaSelectionLong",
"children": []
},
{
"name": "@id",
"text": "id2",
"children": []
}
]
},
{
"name": "p",
"text": "Some random text 2",
"children": [
{
"name": "@id",
"text": "id3",
"children": []
},
{
"name": "contenttype",
"text": "helptext",
"children": []
}
]
}
]
}
Could anyone tell me if the below JSON path makes sense. Just an update to the above JSON path?
$.children[?(@.name=='p' && @.children[?(@.name=='contenttype')].text!='helptext')].children[?(@.name=='@id')].text
Apparantly, jsonpath.com doesn't seem to properly parse this and give me the expected result. I know that it logically makes sense. But, is the syntax fine?
Upvotes: 0
Views: 1259
Reputation: 2280
If you are using Jayway JsonPath java port you can use the Filter Operator nin
JSONPath
$.children[?('helptext' nin @.children[*].text)].children[?(@.name=='@id')].text
Online Test Tool : Jayway JsonPath Evaluator
Upvotes: 1