JSON Path evaluation
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
JSON Path evaluation
In the property transfer "$.persons[*].phoneNumbers[0].type" JSON path is evaluated against the following JSON to extract values.
It gives me value [iPhone]
But I'm expecting value ["iPhone","iPhone","iPhone"], which is the one I'm getting from online json path evaluators.
Can someone explain why the difference and what should I do to get the expected results.
{
"persons": [
{
"firstName": "John",
"lastName": "doe",
"age": 26,
"address": {
"streetAddress": "naist street",
"city": "Nara",
"postalCode": "630-0192"
},
"phoneNumbers": [
{
"type": "iPhone",
"number": "0123-4567-8888"
},
{
"type": "home",
"number": "0123-4567-8910"
}
]
},
{
"firstName": "John",
"lastName": "doe",
"age": 26,
"address": {
"streetAddress": "naist street",
"city": "Nara",
"postalCode": "630-0192"
},
"phoneNumbers": [
{
"type": "iPhone",
"number": "0123-4567-8888"
},
{
"type": "home",
"number": "0123-4567-8910"
}
]
},
{
"firstName": "John",
"lastName": "doe",
"age": 26,
"address": {
"streetAddress": "naist street",
"city": "Nara",
"postalCode": "630-0192"
},
"phoneNumbers": [
{
"type": "iPhone",
"number": "0123-4567-8888"
},
{
"type": "home",
"number": "0123-4567-8910"
}
]
}
]
}
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Like this: $.persons[*].phoneNumbers.type
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
It will not give me the expected result
It is giving me following result, which is not the first phone types, rather all the phone types.
[[iPhone, home, iPhone2, home, iPhone3, home]]
What I require is every person's first phone types which is as I mentioned earlier
["iPhone","iPhone","iPhone"]
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
You are right, try this one:
$..phoneNumbers[?(@.type == "iPhone")].type
$..phoneNumbers[?(@.type == "iPhone")].number
*edit: changed expression
But, just to point out, you asked for the type first, not the number.
-->> But I'm expecting value ["iPhone","iPhone","iPhone"], which is the one I'm getting from online json path evaluators.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
If I know the first phone number type is IPhone, then there is no point tryng to extract that info.
What I need to get is First Phone Number from each person.
