JSON (JavaScript Object Notation) is a lightweight data-interchange format that is easy for humans to read and write, and easy for machines to parse and generate. When working with JSON in Java, you may often find yourself needing to output JSON in a more readable, "pretty" format. In this post, we will explore how to accomplish this using the org.json
library.
What is the org.json
Library?
The org.json
library is a simple and popular library for working with JSON in Java. It provides classes for parsing and generating JSON objects, arrays, and more. This library is often included in many Java projects because of its ease of use and flexibility.
Adding the org.json
Library to Your Project
If you're using Maven, you can easily add the org.json
library as a dependency in your pom.xml
file:
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20210307</version> <!-- Check for the latest version -->
</dependency>
For non-Maven projects, you can download the JAR file from Maven Central Repository and add it to your project’s build path.
Printing Pretty JSON
To print JSON in a pretty format, you can use the JSONObject
or JSONArray
classes provided by the org.json
library. Here’s a step-by-step guide on how to do this:
Step 1: Create a JSON Object
First, you need to create a JSONObject
or a JSONArray
. Here's an example of creating a JSONObject
:
import org.json.JSONObject;
public class PrettyJsonExample {
public static void main(String[] args) {
// Create a JSON Object
JSONObject jsonObject = new JSONObject();
jsonObject.put("name", "John Doe");
jsonObject.put("age", 30);
jsonObject.put("city", "New York");
jsonObject.put("isMarried", false);
// Print pretty JSON
printPrettyJson(jsonObject);
}
private static void printPrettyJson(JSONObject jsonObject) {
// Convert JSON Object to string with indentation
String prettyJson = jsonObject.toString(4); // 4 is the indentation level
System.out.println(prettyJson);
}
}
Step 2: Running the Code
When you run the code above, the output will be in a readable format:
{
"name": "John Doe",
"age": 30,
"city": "New York",
"isMarried": false
}
Step 3: Working with JSON Arrays
If you're dealing with a JSONArray
, you can also print it in a pretty format using a similar approach:
import org.json.JSONArray;
public class PrettyJsonArrayExample {
public static void main(String[] args) {
// Create a JSON Array
JSONArray jsonArray = new JSONArray();
jsonArray.put(new JSONObject().put("name", "John Doe").put("age", 30));
jsonArray.put(new JSONObject().put("name", "Jane Smith").put("age", 25));
// Print pretty JSON Array
printPrettyJsonArray(jsonArray);
}
private static void printPrettyJsonArray(JSONArray jsonArray) {
// Convert JSON Array to string with indentation
String prettyJsonArray = jsonArray.toString(4); // 4 is the indentation level
System.out.println(prettyJsonArray);
}
}
Output
Running the above code will yield the following output:
[
{
"name": "John Doe",
"age": 30
},
{
"name": "Jane Smith",
"age": 25
}
]