Modifying an array in a nested JSON object using Jackson

I am trying to figure out how to modify an existing object in a JSON array by specifying the path to this array.

The problem is that I have no idea how to specify the path to the array so that it can be changed. In every method I tried, there are no examples that will help change the array that is in the ObjectNode.

Here is my code right now, in it I am trying to extract and modify an array by specifying the path to the array using pointer notation, however it always returns an array:

Creating a POJO class is not an option.

My array in my json structure is as follows:

{
  RESULTS: {
     ATTACHMENTS: {
       ATTACHMENT: [{object}]
  }
 }
}

code:

   private JsonNode attachmentConversion(JsonNode object){
        final String ATTACHMENT_POINTER = "/RESULTS/ATTACHMENTS/ATTACHMENT"; //Path to the array
        ObjectNode OUTPUT = object.deepCopy();
        OUTPUT.remove("DETAILS");

        //Validate is an array - It properly fetches the array 
        if(object.at(ATTACHMENT_POINTER).isArray()){
            int counter=0;

            //Loop through attachment array - It properly fethes each object in the array
            for(final JsonNode objNode : object.at(ATTACHMENT_POINTER)){
                ObjectNode objectNode = objNode.deepCopy();
                OUTPUT.withArray(ATTACHMENT_POINTER) //HERE IS THE PROBLEM - How do I specify the path to the array I want to replace with a new object in the withArray api?  This always returns back as an empty array, even though the loop above properly fetches the array elements.
                        .set(counter
                                , objectNode
                                .replace("ATTACH_CONTENT"
                                        , xmlToJson.parseXmlToJson(objNode.get("ATTACH_CONTENT")
                                        .asText())));
                counter = counter+1;
            }
        }
        return new ObjectMapper()
                .createObjectNode()
                .set("customertopology", OUTPUT);
    }
+6
source share
2

, , . JSON.

? , , , , Node , .

private static JsonNode attachmentConversion(JsonNode object){



    final String ATTACHMENT_POINTER = "/RESULTS/ATTACHMENTS/ATTACHMENT"; //Path to the array
    ObjectNode OUTPUT = object.deepCopy();
    OUTPUT.remove("DETAILS");

    //Validate is an array - It properly fetches the array
    if(OUTPUT.at(ATTACHMENT_POINTER).isArray()){
        int counter=0;

        //Loop through attachment array - It properly fethes each object in the array
        for(final JsonNode objNode : OUTPUT.at(ATTACHMENT_POINTER)){
            ((ObjectNode)objNode).replace("ATTACH_CONTENT", xmlToJson.parseXmlToJson(objNode.get("ATTACH_CONTENT"));
        }

    }
    return new ObjectMapper()
            .createObjectNode()
            .set("customertopology", OUTPUT);
}
+4

OUTPUT.get("RESULTS").get("ATTACHMENTS").get("ATTACHMENT");

. quickxml

String json = "{\n" +
            "  \"RESULTS\": {\n" +
            "     \"ATTACHMENTS\": {\n" +
            "       \"ATTACHMENT\": [{\"key\": 1}, {\"key\": 2}]\n" +
            "  }\n" +
            " }\n" +
            "}";

ObjectMapper mapper = new ObjectMapper();
JsonNode node = mapper.readTree(json);
node.get("RESULTS").get("ATTACHMENTS").get("ATTACHMENT").forEach(obj -> {
        System.out.println(obj.get("key"));
    });

true .

System.out.println(node.at("/RESULTS/ATTACHMENTS/ATTACHMENT").isArray());
+2

Source: https://habr.com/ru/post/1016612/


All Articles