Here, I want to verify if the user's name is john doe or not.
{
"name": "John doe",
"age": 26,
"address": {
"streetAddress": "naist street",
"city": "Nara"
},
"phoneNumbers": [
{
"type": "iPhone",
"number": "0123-4567-8888"
}
]
}
I have tried $.firstName[?(@.=='John')] and $[?(@.firstName=='John')] but nothing is working.
CodePudding user response:
Since you've tagged the question xpath, the XPath 3.1 solution is
?name = "John doe"
Earlier versions of XPath do not have JSON support.
CodePudding user response:
You have to point what do you want to use jsonpath or xpath. By the samples in you question I can guess that you are trying jsonpath. Since there is no a first name in your json, try this
$[?(@ == 'John doe')] // result ["John doe"]
but i would use just this
var ifJohnExist =user.name.includes("John");
IMHO nobody is using jsonpath this way, jsonpath is created for a search. So if your json is like this
var user=[{
"name": "John doe",
"age": 26,
"address": {
"streetAddress": "naist street",
"city": "Nara"
},
"phoneNumbers": [
{
"type": "iPhone",
"number": "0123-4567-8888"
}
]
}];
then you can use jsonpath, to find an array element that contains that name
$[?(@.name.includes('John'))].name //result ["John doe"]
and remember that json path is always returning an array