Hey all I am trying to figure out how to go about updating a part of my json. So far I have not found anywhere that shows me how to do this.
For an example, this is what I am needing to update:
{
"lastUpdated": "",
"firetvs": [
{
"name": "lr",
"ip": "",
"mac": "",
"token": ""
},
{
"name": "mbr",
"ip": "",
"mac": "",
"token": ""
},...ETC....
So I am needing to update the data under the
"name": "mbr"
"mac": ""
part which I would need to update the mac address to something like:
"name": "mbr"
"mac": "C1:41:Q4:E8:S1:98:V1"
I have found code that allows me to update a standard value like my lastUpdated but not for something that’s part of an array:
String jsonObject = "{" +
""lastUpdated": ""," +
""firetvs": [" +
"{" +
""name": "lr"," +
""ip": ""," +
""mac": ""," +
""token": """ +
"}," +
"{" +
""name": "mbr"," +
""ip": ""," +
""mac": ""," +
""token": """ +
"}" +
"]" +
"}";
ObjectMapper objectMapper = new ObjectMapper();
ObjectNode objectNode = null;
try {
objectNode = objectMapper.readValue(jsonObject, ObjectNode.class);
objectNode.put("lastUpdated", "02-08-2024");
Log.d("test", objectNode.toPrettyString());
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
I did find this:
objectNode.with("firetvs").put("mac", "C1:41:Q4:E8:S1:98:V1");
But that wont work since its just going to find the first instance of "mac" and update that value instead of updating the 2nd instance of it. And I also get an error of:
Property ‘firetvs’ has value that is not of type ObjectNode (but com.fasterxml.jackson.databind.node.ArrayNode)
So, how would I accomplish this?
2
Answers
You may try JSON library Josson.
https://github.com/octomix/josson
Output
Assuming that you know the indexes of the elements in
firetvs
to update, you could retrieve thefiretvs
property as anArrayNode
, instead of aJsonNode
, and update the mac address only for the elements whose index matches the desired ones (in my demo I’ve added the desired indexes in aSet<Integer>
).On a side note, the
JsonNode.with()
has been deprecated in favor ofJsonNode.withObject()
.In this sample, I’ve added only a couple of elements within
firetvs
for demonstration’s sake. Here, at oneCompiler there’s a broader example.