2

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 :)

1 Answer 1

1
$.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
Sign up to request clarification or add additional context in comments.

1 Comment

i am able to get the values using filters, How to set that particulat object using this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.