Reputation: 23
I have a nested JSON document , I want to set a particular value in the embedded JSON Array,
For Example:-
{
"name":"Jack",
"Address":{
"secondaryAddress":[{"name":"JOHN's Address",
"street":"new ave",
"city":"Canada",
"mobile":123456789
},
{"name":"Selena's Address",
"street":"second ave",
"city":"Canada",
"mobile":987654321
},
{"name":"Jack's Address",
"street":"third ave",
"city":"Canada"
}],
"primaryAddress":{},
}
}
I want to change the mobile number from 987654321 to 456789123 in that particular secondaryAddress object which has mobile number as 987654321.
i am using jsonpath in my project to get / set values To get i would use :-
$.Address.secondaryAddress[*].mobile
which would return a array of mobile numbers like:-
[
123456789,
987654321
]
similarly for set too,
but i am not sure how to go ahead with this scenario where i want to set a particular object in array.
I tried using this but it still sets the value for all array objects :-
DocumentContext cxt = JsonPath.parse(jsonString);
cxt.set("$.Address.secondaryAddress[*].mobile",456789123,Criteria.where("$.Address.secondaryAddress[*].mobile").is("987654321");
Please Help.
Thanks in advance :)
Upvotes: 2
Views: 819
Reputation: 3673
$.Address.secondaryAddress.[?(@.mobile=='987654321')]
Above jsonpath query will give you particular secondaryAddress node with mobile = 987654321 Use below expression to set the new mobile value-
$.Address.secondaryAddress.[?(@.mobile=='987654321')].mobile
Upvotes: 1