Altering an array in a nested JSON object using Jackson

user2100493 :

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

The problem is I have no idea how to specify the path to the array so it can be altered. Every method I've tried there are no examples that go into how to change an array that's in an ObjectNode.

Here is my code right now, in it I attempt to fetch and alter the array by specifying the path to the array via pointer notation, however, this always returns no array:

Building 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);
    }
user2241537 :

I think that you don't want to change the object being passed to the method, So you have made a deep copy. And you want to return a new JSON object.

Am I right so far? If yes, As you have made the deep copy, use it, loop through each element of the array of your concern and replace the Node value with what you desire.

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);
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=463582&siteId=1