This post introduces Json Tree Model Example using Jackson API. Actually, JSON can be represented as a Tree (similar to DOM tree). Therefore, we are possible to traverse to individual nodes in the tree in order to access/edit their values.
Other interesting posts you may like
Let’s start to build Json Tree Model Example
JACKSON dependency in pom.xml
There are several jar in JACKSON library. For this example, we are only using databind.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.javabycode.jackson</groupId> <artifactId>jackson-json</artifactId> <version>1.0.0-SNAPSHOT</version> <name>JSON-EXAMPLE - ${project.artifactId}</name> <packaging>jar</packaging> <dependencies> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.8.0</version> </dependency> </dependencies> </project> |
Jackson framework provides api to do it. JsonNode refers to an individual node in tree which can be accessed using node name. And we are using readTree and writeTree of ObjectMapper object to read and write JSON tree.
Let’s see how it can be done in Java
Json input
We will preparing the file fruit.json with content such as
{“car”:”Audi”,”model”:”2010″,”price”:”30000″,”colors”:[“Grey”,”White”,”Black”]}
Reading JSON Tree using Tree Model
Let’s see the below code snippet to know read Json tree using tree model
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
package com.javabycode.jackson; import java.io.File; import java.io.IOException; import java.util.Iterator; import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; public class JsonJacksonTreeMappingExample { public static void main(String args[]) throws JsonParseException, JsonMappingException, IOException{ ObjectMapper mapper = new ObjectMapper(); /** * Read values from conceptual JSON tree */ JsonNode rootNode = mapper.readTree(new File("C:\\temp\\fruit.json")); JsonNode nameNode = rootNode.path("name"); System.out.println(nameNode.asText()); JsonNode costNode = rootNode.path("cost"); System.out.println(costNode.asText()); JsonNode producebyNode = rootNode.path("produceby"); Iterator<JsonNode> produceby = producebyNode.elements(); while(produceby.hasNext()){ System.out.print(produceby.next().asText()+" "); } System.out.println(); JsonNode fruitPackageNode = rootNode.path("fruitPackage"); Iterator<JsonNode> packages = fruitPackageNode.elements(); while(packages.hasNext()){ System.out.print(packages.next().asText()); System.out.print(", "); System.out.print(packages.next().asText()); } } } |
Here is the output:
Writing JSON Tree using Tree Model
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
package com.javabycode.jackson; import java.io.IOException; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ObjectNode; public class JsonJacksonTreeMappingExample { public static void main(String args[]) throws JsonParseException, JsonMappingException, IOException{ ObjectMapper mapper = new ObjectMapper(); /** * Write values in JSON tree */ JsonGenerator generator = mapper.getFactory().createGenerator(System.out); JsonNode treeRootNode = mapper.createObjectNode(); ((ObjectNode) treeRootNode).put("name", "Orange"); ((ObjectNode) treeRootNode).put("cost", "75"); ArrayNode produceByNode = ((ObjectNode) treeRootNode).putArray("produceby"); produceByNode.add("England"); produceByNode.add("France"); produceByNode.add("Canada"); ObjectNode objectNode = ((ObjectNode) treeRootNode).putObject("fruitPackage"); objectNode.put("name", "Fruit Punnets"); objectNode.put("quantityNet", "5"); //mapper.writeTree(generator, treeRootNode); String pretty = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(treeRootNode); System.out.println(pretty); } } |
Here is the output
That’s it. You also refer to other post Convert Java Object to/from JSON using JACKSON API