skip to Main Content

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


  1. You may try JSON library Josson.

    https://github.com/octomix/josson

    Josson josson = Josson.fromJsonString(
        "{" +
            ""lastUpdated": ""," +
            ""firetvs": [" +
                "{" +
                    ""name": "lr"," +
                    ""ip": ""," +
                    ""mac": ""," +
                    ""token": """ +
                "}," +
                "{" +
                    ""name": "mbr"," +
                    ""ip": ""," +
                    ""mac": ""," +
                    ""token": """ +
                "}" +
            "]" +
        "}");
    Map<String, JsonNode> params = Map.of(
        "$date", TextNode.valueOf("02-08-2024"),
        "$key", TextNode.valueOf("mbr"),
        "$val", TextNode.valueOf("C1:41:Q4:E8:S1:98:V1")
    );
    JsonNode node = josson.getNode("field(lastUpdated:$date, firetvs.field(mac:if([name=$key],$val,mac)))", params);
    System.out.println(node.toPrettyString());
    

    Output

    {
      "lastUpdated" : "02-08-2024",
      "firetvs" : [ {
        "name" : "lr",
        "ip" : "",
        "mac" : "",
        "token" : ""
      }, {
        "name" : "mbr",
        "ip" : "",
        "mac" : "C1:41:Q4:E8:S1:98:V1",
        "token" : ""
      } ]
    }
    
    Login or Signup to reply.
  2. Assuming that you know the indexes of the elements in firetvs to update, you could retrieve the firetvs property as an ArrayNode, instead of a JsonNode, 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 a Set<Integer>).

    On a side note, the JsonNode.with() has been deprecated in favor of JsonNode.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.

    public class Main {
        public static void main(String[] args) throws JsonProcessingException {
            String json = "{n" +
                    "    "lastUpdated": "",n" +
                    "    "firetvs": [n" +
                    "        {n" +
                    "            "name": "lr",n" +
                    "            "ip": "",n" +
                    "            "mac": "",n" +
                    "            "token": ""n" +
                    "        },n" +
                    "        {n" +
                    "            "name": "mbr",n" +
                    "            "ip": "",n" +
                    "            "mac": "",n" +
                    "            "token": ""n" +
                    "        }n" +
                    "    ]n" +
                    "}";
    
            String mac = "C1:41:Q4:E8:S1:98:V1";
            Set<Integer> indexesToUpdate = new HashSet<>(Set.of(1, 3, 4));
    
            ObjectMapper objectMapper = new ObjectMapper();
            ObjectNode rootNode = objectMapper.readValue(json, ObjectNode.class);
            ArrayNode firetvsNode = (ArrayNode) rootNode.get("firetvs");
            indexesToUpdate.stream().forEach(i -> ((ObjectNode) firetvsNode.get(i)).put("mac", mac));
    
            String jsonMacReplaced = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(rootNode);
            System.out.println(jsonMacReplaced);
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search