0

I am using https://github.com/ralfstx/minimal-json as the json library for this project and I have a json input that has a nested array with key value pairs inside of the array.

The Json looks like this:

{
"info":[  
  {  
     "name":"thing1",
     "value":"value1"
  },
  {  
     "name":"user",
     "value":"admin"
  },
  {  
     "name":"password",
     "value":""
  }
  ],
   "logonStyle":null,
   "logonUXVersion":0
}

So I have tried doing things like:

jsonRes.get("info").set("password", "tothisvalue");
jsonRes.set("password", "tothisvalue");

but neither of those ways work. I noticed from this Q&A it had a different library where you could chain them : How to access nested elements of json object using getJSONArray method

Wondering if I could make a case to add in this library or if someone knows a way to set a value in the nested array.

Hudspeth
  • 146
  • 13

1 Answers1

0

Figured it out.

JsonArray connectionInfo = requestBody.get("connectionParamInfo").asArray();
    for (JsonValue x : connectionInfo) {
        if (x.equals(jsonStr)) {
            x.asObject().set("value", newPassword);
        }
    }

If I modify the requestBody which is a JsonObject in this case, the updates that I made to the values within the part that I pulled out "connectionInfo" it actually goes back into the original requestBody.

The whole time I thought I had to merge something back in. When in fact it was already updating my json.

Hudspeth
  • 146
  • 13