JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

Enable JSON pretty print using Jackson API


This page shows how to enable JSON pretty print using Jackson APIs.

Note: Refer How to convert Java object to JSON string? page for dependent libraries.

package com.java2novice.json.examples;

import java.io.IOException;

import org.codehaus.jackson.map.ObjectMapper;

import com.java2novice.json.models.Employee;

public class JsonPrettyPrintEx {

	public static void main(String a[]){
		
		Employee emp = new Employee();
		ObjectMapper mapperObj = new ObjectMapper();
		
		try {
			// get Employee object as a json string
			String jsonStr = mapperObj.writeValueAsString(emp);
			System.out.println("json output in compact mode:\n");
			System.out.println(jsonStr);
			String jsonPrettyStr 
					= mapperObj.writerWithDefaultPrettyPrinter().writeValueAsString(emp);
			System.out.println("\n\njson output in pretty print mode:\n");
			System.out.println(jsonPrettyStr);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

Output:
json output is in compact mode:

{"empId":1016,"name":"Nataraja Gootooru","designation":"Programmer","department":"Java2Novice","salary":20000}


json output is in pretty print mode:

{
  "empId" : 1016,
  "name" : "Nataraja Gootooru",
  "designation" : "Programmer",
  "department" : "Java2Novice",
  "salary" : 20000
}
<< Previous Program | Next Program >>

Jackson JSON examples

  1. How to convert Java object to JSON string?
  2. How to convert JSON string to Java object?
  3. How to convert JSON string to Map using Jackson API?
  4. How to convert Map to JSON string using Jackson API?
  5. Enable JSON pretty print using Jackson API
  6. How to rename JSON properties using Jackson annotations?
  7. How to ignore JSON property using Jackson annotations?
  8. How to order JSON elements using Jackson annotations?
  9. How to ignore json empty or null values using Jackson API in java?
  10. How to handle date in Json using Jackson api in java?
  11. How to read specific json node in Jackson api (tree model)?
  12. Jackson API client - how to read json from URL?
Knowledge Centre
What is race condition?
A race condition is a situation in which two or more threads or processes are reading or writing some shared data, and the final result depends on the timing of how the threads are scheduled. Race conditions can lead to unpredictable results and subtle program bugs. A thread can prevent this from happening by locking an object. When an object is locked by one thread and another thread tries to call a synchronized method on the same object, the second thread will block until the object is unlocked.
Famous Quotations
Millions long for immortality who do not know what to do with themselves on a rainy Sunday afternoon.
-- Susan Erz

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2022 by Nataraja Gootooru. All Rights Reserved.