cancel
Showing results for 
Search instead for 
Did you mean: 

JSON Path evaluation

New Contributor

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"
}
]
}
]
}

 

 

 

4 REPLIES 4
Contributor

Re: JSON Path evaluation

Remove the [0] behind phoneNumbers, since it only retrieves the very first occurens.

Like this: $.persons[*].phoneNumbers.type
New Contributor

Re: JSON Path evaluation

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"]

 

 

 

Contributor

Re: JSON Path evaluation

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.

New Contributor

Re: JSON Path evaluation

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.

 

 

New Here?
Join us and watch the welcome video:
6 / 7 API Testing Mistakes Video
Top Kudoed Authors