How do I pretty-print existing JSON data with Java? [closed]
Asked Answered
E

7

67

I have a compact JSON string, and I want to format it nicely in Java without having to deserialize it first -- e.g. just like jsonlint.org does it. Are there any libraries out there that provides this?

A similar solution for XML would also be nice.

Eldwun answered 31/5, 2011 at 9:9 Comment(6)
By pretty print, do you mean color-code, indent, or both?Teufert
JSONObject has a toString(int) where you provide spacing for pretty printing.Remind
@The Elite Gentleman: I would need to deserialize the JSON data to get a JSONObject, wouldn't I?Eldwun
@neu242, no, JSONObject is a JSON data already. Why do you want to do that?Remind
GWT 2.5.0 doesn't have a toString(int) on JSONObjectFord
I would look at Nashorn (part of a java installation). #4106295Congruous
L
4

I think for pretty-printing something, it's very helpful to know its structure.

To get the structure you have to parse it. Because of this, I don't think it gets much easier than first parsing the JSON string you have and then using the pretty-printing method toString mentioned in the comments above.

Of course you can do similar with any JSON library you like.

Lugsail answered 31/5, 2011 at 9:45 Comment(1)
Links are broken.Gamopetalous
P
98
int spacesToIndentEachLevel = 2;
new JSONObject(jsonString).toString(spacesToIndentEachLevel);

Using org.json.JSONObject (built in to JavaEE and Android)

Parity answered 18/10, 2014 at 17:57 Comment(9)
What library does this use? As is it is not a very helpful answer.Slut
@Slut Backer, this is not a library. As Heath Borders said, it is built into the Android libraries. I've tested this and it works perfectly without any libraries.Franklynfrankness
Hm... I'm not developing for Android, and I don't deploy to a java EE container... hence the need for clarification. Thanks for adding that :) So for someone like me, distributing a java app targeting SE, it would be a library such as org.json:json:20090211?Slut
for those of us using Android, this is very useful and not really clear in the quick view of the docs. Didn't expect it to be built in.Muraida
org.json.JSONObject is not built into JavaEE. The JSON-P library for JsonObject is javax.json.JsonObject.Remind
greattttt, this is very usefulMandle
The dependency with group ID of org.json and artifact ID of json in the Maven POM file or in your Gradle also supports prettifying JSON data by passing in the indent level in the toString(int) method.Sussman
This is really useful in Spring Boot.Housing
this is very useful. but one problem. if data is large, all data not showing in. Any suggestionErstwhile
N
24

Use gson. https://www.mkyong.com/java/how-to-enable-pretty-print-json-output-gson/

Gson gson = new GsonBuilder().setPrettyPrinting().create();
String json = gson.toJson(my_bean);

output

{
  "name": "mkyong",
  "age": 35,
  "position": "Founder",
  "salary": 10000,
  "skills": [
    "java",
    "python",
    "shell"
  ]
}
Neutrality answered 23/7, 2017 at 13:24 Comment(0)
A
15

Another way to use gson:

String json_String_to_print = ...
Gson gson = new GsonBuilder().setPrettyPrinting().create();
JsonParser jp = new JsonParser();
return gson.toJson(jp.parse(json_String_to_print));

It can be used when you don't have the bean as in susemi99's post.

Affectional answered 22/8, 2017 at 15:36 Comment(3)
Instantiating JsonParser is now deprecated. Use the static method JsonParser.parseString(json_String_to_print) instead.Vanda
This is the right answer let's up vote it !!Inhibitor
this must be accepted answer!Peritonitis
W
11

If you are using jackson you can easily achieve this with configuring a SerializationFeature in your ObjectMapper:

com.fasterxml.jackson.databind.ObjectMapper mapper = new ObjectMapper();

mapper.configure(SerializationFeature.INDENT_OUTPUT, true);

mapper.writeValueAsString(<yourObject>);

Thats it.

Whiten answered 19/2, 2014 at 16:0 Comment(0)
L
4

I think for pretty-printing something, it's very helpful to know its structure.

To get the structure you have to parse it. Because of this, I don't think it gets much easier than first parsing the JSON string you have and then using the pretty-printing method toString mentioned in the comments above.

Of course you can do similar with any JSON library you like.

Lugsail answered 31/5, 2011 at 9:45 Comment(1)
Links are broken.Gamopetalous
O
2

I fount a very simple solution:

<dependency>
    <groupId>com.cedarsoftware</groupId>
    <artifactId>json-io</artifactId>
    <version>4.5.0</version>
</dependency>

Java code:

import com.cedarsoftware.util.io.JsonWriter;
//...
String jsonString = "json_string_plain_text";
System.out.println(JsonWriter.formatJson(jsonString));
Oresund answered 18/8, 2016 at 8:9 Comment(0)
G
2

Underscore-java library has methods U.formatJson(json) and U.formatXml(xml).

Gamopetalous answered 17/8, 2019 at 10:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.