Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing JSON elements with jackson

Tags:

java

json

jackson

I've a particular JSON Node that corresponds to import org.codehaus.jackson.JsonNode, and not import org.codehaus.jackson.map.JsonNode.

[     {         "givenName": "Jim",         "formattedName": "jimJackson",         "familyName": null,         "middleName": "none",         "honorificPrefix": "mr",         "honorificSuffix": "none"     },     {         "givenName": "john",         "formattedName": "johnLasher",         "familyName": null,         "middleName": "none",         "honorificPrefix": "mr",         "honorificSuffix": "none"     },     {         "givenName": "carlos",         "formattedName": "carlosAddner",         "familyName": null,         "middleName": "none",         "honorifiPrefix": "mr",         "honorificSuffix": "none"     },     {         "givenName": "lisa",         "formattedName": "lisaRay",         "familyName": null,         "middleName": "none",         "honorificPrefix": "mrs",         "honorificSuffix": "none"     },     {         "givenName": "bradshaw",         "formattedName": "bradshawLion",         "familyName": null,         "middleName": "none",         "honorificPrefix": "mr",         "honorificSuffix": "none"     },     {         "givenName": "phill",         "formattedName": "phillKane",         "familyName": null,         "middleName": "none",         "honorificPrefix": "mr",         "honorificSuffix": "none"     },     {         "givenName": "Gabriel",         "formattedName": "gabrielMoosa",         "familyName": null,         "middleName": "none",         "honorificPrefix": "mr",         "honorificSuffix": "none"     } ] 

I want to remove the "familyName" and "middleName" from all the JSON nodes of the above array. Is there any way to achieve this?

like image 724
R Syed Avatar asked Mar 24 '13 19:03

R Syed


People also ask

How do I remove a specific element from a JSON array?

You can remove an element from the JSONArray object using the remove() method. This method accepts an integer and removes the element in that particular index.

What does Jackson ObjectMapper do?

The Jackson ObjectMapper can parse JSON from a string, stream or file, and create a Java object or object graph representing the parsed JSON. Parsing JSON into Java objects is also referred to as to deserialize Java objects from JSON. The Jackson ObjectMapper can also create JSON from Java objects.

What is JsonNode Jackson?

JsonNode is Jackson's tree model (object graph model) for JSON. Jackson can read JSON into a JsonNode instance, and write a JsonNode out to JSON. This Jackson JsonNode tutorial will explain how to deserialize JSON into a JsonNode and serialize a JsonNode to JSON.


2 Answers

I haven't tested this, but I think something like this would do what you want:

import org.codehaus.jackson.node.ObjectNode; // ... for (JsonNode personNode : rootNode) {     if (personNode instanceof ObjectNode) {         ObjectNode object = (ObjectNode) personNode;         object.remove("familyName");         object.remove("middleName");     } } 

You could also do this more efficiently using Jackon's raw parsing API, but the code would be a lot messier.

like image 80
gsteff Avatar answered Sep 20 '22 10:09

gsteff


ObjectMapper of Jackson gives solution with only few steps.

Save the json data in a file say 'data.json'. Copy following the code into a function without import statements and invoke the function. The resulting JSON will be written into a new file 'data1.json'.

import java.io.File; import java.io.IOException;  import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ObjectNode; ObjectMapper objectMapper = new ObjectMapper();         JsonNode jsonNode = objectMapper.readTree(new File("data.json"));         for (JsonNode node : jsonNode) {             ((ObjectNode)node).remove("familyName");             ((ObjectNode)node).remove("middleName");         }         objectMapper.writeValue(new File("data1.json"), jsonNode); 
like image 36
Nayana Shekar C Avatar answered Sep 20 '22 10:09

Nayana Shekar C