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?
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.
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.
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.
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With