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.
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.
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.
int spacesToIndentEachLevel = 2;
new JSONObject(jsonString).toString(spacesToIndentEachLevel);
Using org.json.JSONObject
(built in to JavaEE and Android)
org.json:json:20090211
? –
Slut org.json.JSONObject
is not built into JavaEE. The JSON-P
library for JsonObject
is javax.json.JsonObject
. –
Remind 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 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"
]
}
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.
JsonParser
is now deprecated. Use the static method JsonParser.parseString(json_String_to_print)
instead. –
Vanda 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.
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.
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));
Underscore-java library has methods U.formatJson(json)
and U.formatXml(xml)
.
© 2022 - 2024 — McMap. All rights reserved.
toString(int)
where you provide spacing for pretty printing. – Remind