[Jackson API Examples] - How To Create A JSON String Using JsonGenerator in Jackson API

#JavaInspires

Hi Guys,

Welcome to Java Inspires.



In this post, we will see program to create a JSON string using JsonGenerator object from Jackson.

We will create this JSON using JsonGenerator.


{
  "name": "Elon Musk",
  "age": 45,
  "working for": [
    {
      "name": "Space X"
    },
    {
      "name": "Tesla"
    },
    {
      "name": "The Boring Company"
    }
  ]
}


Here, we are using jackson dependency in our maven porject.
pom.xml
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.javainspires</groupId>
	<artifactId>jackson-examples</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<dependencies>

		<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-databind</artifactId>
			<version>2.12.2</version>
		</dependency>

	</dependencies>
</project>



MainApp.java



package com.javainspires;

import java.io.IOException;
import java.io.StringWriter;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;

/**
 * 
 * @author #JavaInspires
 *
 */
public class MainApp {

	public static void main(String[] args) throws IOException {

		// How to create a JSON String using JsonGenerator in Jackson API

		// create Json Factory object
		JsonFactory jsonFactory = new JsonFactory();
		// create StringWriter object
		StringWriter stringWriter = new StringWriter();

		// create Json Generator
		JsonGenerator jsonGenerator = jsonFactory.createGenerator(stringWriter);

		// enable pretty print
		jsonGenerator.useDefaultPrettyPrinter();

		// start json object
		jsonGenerator.writeStartObject();

		// write a field to json
		jsonGenerator.writeStringField("name", "Elon Musk");
		jsonGenerator.writeNumberField("age", 45);

		jsonGenerator.writeFieldName("working for");

		// write array to json
		jsonGenerator.writeStartArray();
		// object 1
		jsonGenerator.writeStartObject();
		jsonGenerator.writeStringField("name", "Space X");
		jsonGenerator.writeEndObject();
		// object 2
		jsonGenerator.writeStartObject();
		jsonGenerator.writeStringField("name", "Tesla");
		jsonGenerator.writeEndObject();
		// object 3
		jsonGenerator.writeStartObject();
		jsonGenerator.writeStringField("name", "The Boring Company");
		jsonGenerator.writeEndObject();
		// end of array
		jsonGenerator.writeEndArray();

		// end json object
		jsonGenerator.writeEndObject();

		// close jsongenerator
		jsonGenerator.close();

		// print json string
		System.out.println(stringWriter.toString());

	}
}


Output:


{
  "name" : "Elon Musk",
  "age" : 45,
  "working for" : [ {
    "name" : "Space X"
  }, {
    "name" : "Tesla"
  }, {
    "name" : "The Boring Company"
  } ]
}




Post a Comment

Previous Post Next Post