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";
ObjectNode OUTPUT = object.deepCopy();
OUTPUT.remove("DETAILS");
if(object.at(ATTACHMENT_POINTER).isArray()){
int counter=0;
for(final JsonNode objNode : object.at(ATTACHMENT_POINTER)){
ObjectNode objectNode = objNode.deepCopy();
OUTPUT.withArray(ATTACHMENT_POINTER)
.set(counter
, objectNode
.replace("ATTACH_CONTENT"
, xmlToJson.parseXmlToJson(objNode.get("ATTACH_CONTENT")
.asText())));
counter = counter+1;
}
}
return new ObjectMapper()
.createObjectNode()
.set("customertopology", OUTPUT);
}
source
share