Java – How to pretty-print JSON using ObjectMapper ?

In this article, we will discuss how to pretty print JSON using Jackson or ObjectMapper

1. Required libraries :

1.1 Maven Co-ordinates :

<dependency>
			<groupId>com.fasterxml.jackson.dataformat</groupId>
			<artifactId>jackson-dataformat-yaml</artifactId>
			<version>2.13.4</version>
</dependency>
		
<dependency>
		    <groupId>com.fasterxml</groupId>
		    <artifactId>jackson-xml-databind</artifactId>
		    <version>0.6.2</version>
</dependency>

1.2 JAR files to download :

Download below jar files and include them in Project classpath

2. Pretty print JSON :

In the below illustration, we will print JSON in a structured format using ObjectMapper,

  • 1st step is to read the contents of the JSON String
  • 2nd step is to instantiate ObjectMapper and invoke readValue() method of ObjectMapper by passing 2 input-arguments which returns an Object,
    • JSON String
    • Object-type
  • 3rd step is to invoke below methods for pretty printing JSON in a structured format and pass Object obtained in the above step,
    • writerWithDefaultPrettyPrinter()
    • writeValueAsString(object)

JSON file contents – Person.json :

{
  "title" : "Mr.",
  "firstName" : "Sam",
  "lastName" : "Anton",
  "address" : {
    "flatNumber" : "BV-1025",
    "buildingName" : "Shivaji",
    "plotNumber" : "1093",
    "sector" : "Sector 19",
    "city" : "Bengaluru",
    "state" : "Karnataka",
    "country" : "India",
    "nodeName" : "South Bengaluru"
  },
  "items" : [ {
    "itemName" : "Television",
    "itemBoughtYear" : "2014"
  }, {
    "itemName" : "Washing Machine",
    "itemBoughtYear" : "2020"
  }, {
    "itemName" : "Refrigerator",
    "itemBoughtYear" : "2011"
  }, {
    "itemName" : "Grinder",
    "itemBoughtYear" : "2012"
  }, {
    "itemName" : "Computer",
    "itemBoughtYear" : "2010"
  } ]
}

PrettyPrintJSON2.java

package in.bench.resources.yaml.conversion;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

import com.fasterxml.jackson.databind.ObjectMapper;

public class PrettyPrintJSON2 {

	public static void main(String[] args) {

		try {
			// read JSON String
			String jsonContent = new String(Files.readAllBytes(Paths.get(
					"C:\\Users\\Demo\\eclipse-workspace\\YamlConversion\\Person.json")));


			// instantiate ObjcetMapper
			ObjectMapper mapper = new ObjectMapper();
			Person person = mapper.readValue(jsonContent, Person.class);


			// convert Person object to JSON String
			String jsonString = mapper
					.writerWithDefaultPrettyPrinter()
					.writeValueAsString(person);


			// print to console
			System.out.println("Pretty print JSON :- \n\n" + jsonString);

		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

Output in console :

Pretty print JSON :- 

{
  "title" : "Mr.",
  "firstName" : "Sam",
  "lastName" : "Anton",
  "address" : {
    "flatNumber" : "BV-1025",
    "buildingName" : "Shivaji",
    "plotNumber" : "1093",
    "sector" : "Sector 19",
    "city" : "Bengaluru",
    "state" : "Karnataka",
    "country" : "India",
    "nodeName" : "South Bengaluru"
  },
  "items" : [ {
    "itemName" : "Television",
    "itemBoughtYear" : "2014"
  }, {
    "itemName" : "Washing Machine",
    "itemBoughtYear" : "2020"
  }, {
    "itemName" : "Refrigerator",
    "itemBoughtYear" : "2011"
  }, {
    "itemName" : "Grinder",
    "itemBoughtYear" : "2012"
  }, {
    "itemName" : "Computer",
    "itemBoughtYear" : "2010"
  } ]
}

In the following article, we will discuss same example using JSONObject in detail

Related Articles :

References :

Happy Coding !!
Happy Learning !!

Java – How to convert XML to YAML using Jackson ?
Java – How to pretty-print JSON using JSONObject ?