I do have this sample json data that I got from jsonplaceholder, a dummy rest api data. I am practicing display/fetching data to it.
Now, I am having a trouble when I am fetching the name of the person. But look at the company, it also has the attribute name to it.
[
{
"id": 1,
"name": "Leanne Graham",
"username": "Bret",
"email": "[email protected]",
"address": {
"street": "Kulas Light",
"suite": "Apt. 556",
"city": "Gwenborough",
"zipcode": "92998-3874",
"geo": {
"lat": "-37.3159",
"lng": "81.1496"
}
},
"phone": "1-770-736-8031 x56442",
"website": "hildegard.org",
"company": {
"name": "Romaguera-Crona",
"catchPhrase": "Multi-layered client-server neural-net",
"bs": "harness real-time e-markets"
}
},
so whenever I try to use $..name
it displays the company name as well, which is I do not want I only want the name of the people. How do I separate it the name of the person and the name of the company.
I am using jsonpath as my playground to practice.
any ideas are greatly appreciated!
2
Answers
You can just use this following script:
In your case, I got this output:
With this Json:
The jsonpath for this query would be
$[*].name
which means "for any element in the root array get thename
field". This avoids using..
which is interpreted as recursive descent (i.e. looking at any and all child objects).