Creating and Writing JSON object model to a file

This post explains how to create and save json object model to a file.

In the example we build the below json object model representing person information.

  "ssn" : "111111111",
  "name" : {
    "firstName" : "Ronald",
    "lastName" : "Childs"
  "phoneNumbers" : [ "1112223333", "4445556666" ]

The main code is as shown below

Main Code

1  package package10;
3  import;
4  import;
5  import;
7  import com.fasterxml.jackson.core.JsonFactory;
8  import com.fasterxml.jackson.core.JsonGenerator;
9  import com.fasterxml.jackson.core.JsonProcessingException;
10 import com.fasterxml.jackson.databind.ObjectMapper;
11 import com.fasterxml.jackson.databind.node.ArrayNode;
12 import com.fasterxml.jackson.databind.node.ObjectNode;
14 public class TreeModelDemo2 {
15  public static void main(String[] args) throws JsonProcessingException, IOException {
16      File file = new File("jsondata6.json");
17      JsonFactory jsonFactory = new JsonFactory();
19         try(FileWriter fw = new FileWriter(file);) {
20              ObjectMapper objectMapper = new ObjectMapper();
22              ObjectNode parentNode = objectMapper.createObjectNode();
23              parentNode.put("ssn", "111111111");
24              ObjectNode childObjectNode = parentNode.putObject("name");
25              childObjectNode.put("firstName", "Ronald");
26              childObjectNode.put("lastName", "Childs");
28              ArrayNode arrayNode = parentNode.putArray("phoneNumbers");
29              arrayNode.add("1112223333");
30              arrayNode.add("4445556666");
32             JsonGenerator jsonGenerator = jsonFactory.createGenerator(fw);
33             jsonGenerator.useDefaultPrettyPrinter();
34             objectMapper.writeTree(jsonGenerator, parentNode);
35         }
36      }
37 }


We need an instance of ObjectMapper to create json object model. So we create it. Refer to line 20.

We create an object node (which will be parent node i.e., person object) by calling objectMapper.createObjectNode method (Refer to line 22). This method will return an instance of ObjectNode class.

We then set properties of person object which is ssn, name, and phoneNumbers.

“ssn” is simple property. The property name and value is set using put method of ObjectNode class. Refer to line 23.

“name” property is a child of person object. First we set the name of property by calling putObject and passing the property name as argument. ObjectNode’s putObject returns an instance of ObjectNode and is used to represent the name property. Refer to line 24.

“phoneNumbers” property is an array. First we set the name of property by calling putArray and passing the property name as argument. This method returns an instance of ArrayNode.
We add entries to the array, using ArrayNode’s add method. Refer from line 28 to 30.

Now we save the data to file using ObjectMapper’s writeTree method. The writeTree method requires two arguments
1) An instance of JsonGenerator
2) An instance of json object model to be saved (i.e., person in this case)

ObjectMapper uses the jsonGenerator and writes the object model to a file. Refer from line 32 to 34.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s