Quantcast
Channel: Mkyong.com
Viewing all articles
Browse latest Browse all 28

Pretty Print JSON with JSON.simple

$
0
0

This article shows how to pretty print JSON with JSON.simple.

Table of contents:

P.S Tested with json-simple 4.0.1

1. Setup JSON.simple

pom.xml

<dependency>
    <groupId>com.github.cliftonlabs</groupId>
    <artifactId>json-simple</artifactId>
    <version>4.0.1</version>
</dependency>

2. Default Compact Print JSON

By default, JSON.simple prints all JSON in compact mode.

JsonSimplePrettyPrintExample.java

package com.mkyong.json.jsonsimple;

import com.github.cliftonlabs.json_simple.JsonException;
import com.github.cliftonlabs.json_simple.JsonObject;
import com.github.cliftonlabs.json_simple.Jsoner;

public class JsonSimplePrettyPrintExample {

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

        String json = "{\"name\": \"mkyong\", \"age\": 42}";

        // convert JSON to JsonObject
        JsonObject jsonObject = (JsonObject) Jsoner.deserialize(json);

        // default, compact print
        String out = jsonObject.toJson();

        System.out.println(out);

    }
}

Output


{"name":"mkyong","age":42}

3. Pretty Print JSON with JSON.simple

In JSON.simple, we can use Jsoner.prettyPrint() to pretty print JSON.

JsonSimplePrettyPrintExample.java

package com.mkyong.json.jsonsimple;

import com.github.cliftonlabs.json_simple.JsonException;
import com.github.cliftonlabs.json_simple.JsonObject;
import com.github.cliftonlabs.json_simple.Jsoner;

public class JsonSimplePrettyPrintExample {

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

        String json = "{\"name\": \"mkyong\", \"age\": 42}";

        JsonObject jsonObject = (JsonObject) Jsoner.deserialize(json);

        // pretty print
        String out = Jsoner.prettyPrint(jsonObject.toJson());

        // default, compact print
        // String out = jsonObject.toJson();

        System.out.println(out);

    }
}

Output


{
	"name": "mkyong",
	"age": 42
}

4. Write Pretty Print JSON to a File

The following example uses JSON.simple to write pretty print JSON to a file named person.json.

JsonSimplePrettyPrintExample2.java

package com.mkyong.json.jsonsimple;

import com.github.cliftonlabs.json_simple.JsonArray;
import com.github.cliftonlabs.json_simple.JsonException;
import com.github.cliftonlabs.json_simple.JsonObject;
import com.github.cliftonlabs.json_simple.Jsoner;

import java.io.FileWriter;
import java.io.IOException;
import java.io.StringReader;

public class JsonSimplePrettyPrintExample2 {

    public static void main(String[] args) {

        JsonObject jsonObject = new JsonObject();
        jsonObject.put("name", "mkyong");
        jsonObject.put("age", 42);

        JsonArray list = new JsonArray();
        list.add("msg 1");
        list.add("msg 2");
        list.add("msg 3");

        jsonObject.put("messages", list);

        try (FileWriter fileWriter = new FileWriter("person.json")) {

            // default
            // Jsoner.serialize(jsonObject, fileWriter);

            // pretty print , two spaces
            Jsoner.prettyPrint(new StringReader(jsonObject.toJson()),
                    fileWriter,
                    "  ", "\n");

        } catch (IOException | JsonException e) {
            throw new RuntimeException(e);
        }

    }

}

Output

person.json

{
  "name": "mkyong",
  "messages": [
    "msg 1",
    "msg 2",
    "msg 3"
  ],
  "age": 42
}

5. Download Source Code

$ git clone https://github.com/mkyong/java-json

$ cd simplejson

6. References

The post Pretty Print JSON with JSON.simple appeared first on Mkyong.com.


Viewing all articles
Browse latest Browse all 28

Trending Articles