Skip to content
Advertisement

How to create Array of Objects using the Jackson Streaming API

I am trying to create a JSON using the Jackson Streaming API. I know how to create an array of elements in JSON using Jackson as we have plenty of examples related to it. But I am a bit confused about how to create an array of Objects using it.

Following is the JSON structure that I would like to obtain at the end:

{
  "name" : "Batman",
  "year" : 2008,
  "writers":[
    {
      "name" : "Nolan",
      "age"  : 49
    },
    {
      "name" : "Johnathan",
      "age"  : 35
    }
  ]
}

Following is the code I have:

import org.json.JSONObject;

import com.fasterxml.jackson.core.JsonEncoding;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.ObjectMapper;

public class HelloWorld {

    public static void main(String[] args) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        ByteArrayOutputStream jsonStream = new ByteArrayOutputStream();
        JsonGenerator jsonGenerator = mapper.getFactory().createGenerator(jsonStream, JsonEncoding.UTF8);
        jsonGenerator.writeStartObject();
        jsonGenerator.writeStringField("name", "Batman");
        jsonGenerator.writeNumberField("year", 2008);
        jsonGenerator.writeFieldName("writers");
        jsonGenerator.writeStartArray();
        // How to to create here objects and add it to the "writers"
        // Should I create another JsonGenerator and create objects usign it?
        jsonGenerator.writeEndArray();
        jsonGenerator.writeEndObject();
        jsonGenerator.close();
        String jsonData = new String(jsonStream.toByteArray(), "UTF-8");
        JSONObject json = new JSONObject(jsonData);
        System.out.println(json.toString(4));
    }
}

Can someone please guide me on how to create the objects and add them to the array one by one? I am unable to find such an example so posting here.

Advertisement

Answer

After trying a few things I was able to get it. Basically, I had to do the same thing which I was asked in the question. I am not sure why it did not work the first time maybe I missed something. Anyways here is how you can add objects into the array using the Jackson Streaming API. Posting this as it can be beneficial to someone else in the future.

I am creating an array writers in this case and adding the objects into it using the same jsonGenerator.

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import org.json.JSONObject;

import com.fasterxml.jackson.core.JsonEncoding;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.ObjectMapper;

public class HelloWorld {

    public static void main(String[] args) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        ByteArrayOutputStream jsonStream = new ByteArrayOutputStream();
        JsonGenerator jsonGenerator = mapper.getFactory().createGenerator(jsonStream, JsonEncoding.UTF8);
        jsonGenerator.writeStartObject();
        jsonGenerator.writeStringField("name", "Batman");
        jsonGenerator.writeNumberField("year", 2008);
        jsonGenerator.writeFieldName("writers");
        jsonGenerator.writeStartArray();
        jsonGenerator.writeStartObject();
        jsonGenerator.writeStringField("name", "Nolan");
        jsonGenerator.writeNumberField("age", 45);
        jsonGenerator.writeEndObject();
        jsonGenerator.writeStartObject();
        jsonGenerator.writeStringField("name", "Johanathan");
        jsonGenerator.writeNumberField("age", 35);
        jsonGenerator.writeEndObject();
        jsonGenerator.writeEndArray();
        jsonGenerator.writeEndObject();
        jsonGenerator.close();
        String jsonData = new String(jsonStream.toByteArray(), "UTF-8");
        JSONObject json = new JSONObject(jsonData);
        System.out.println(json.toString(4));
    }
}

You will get the output something like this:

{
    "year": 2008,
    "name": "Batman",
    "writers": [
        {
            "name": "Nolan",
            "age": 45
        },
        {
            "name": "Johanathan",
            "age": 35
        }
    ]
}

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement