Grégoire Borel
Grégoire Borel

Reputation: 2028

Remove an array with Jackson

I'm trying to remove an array from a JSON file using Jackson. This is the structure of my JSON file:

[  
   {  
      "Lorem Ipsum ":3,
      "Lorem Ipsum ":14.2,
      "Lorem Ipsum ":5.8
   },
   {  
      "thingToRemove":"stuff"
   }
]

and this is what I've tried, without effect:

for (JsonNode personNode : rootNode) {
  if (personNode instanceof ObjectNode) {
     ObjectNode object = (ObjectNode) personNode;
     object.remove("thingToRemove");
  }
}

SOLUTION:

Add the code written above the following instruction:

jsonFile = rootNode.toString();

Upvotes: 0

Views: 351

Answers (1)

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 280031

You're reading the content of the file in memory and deserialize into a JsonNode data structure with Jackson. This data structure is in memory. You'll need to write it back to disk (to the file) after you remove an element.

Upvotes: 2

Related Questions