Convert JSON String to Pretty Print JSON Output Using Jackson

Convert JSON String to Pretty Print JSON output using Jackson

To indent any old JSON, just bind it as Object, like:

Object json = mapper.readValue(input, Object.class);

and then write it out with indentation:

String indented = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(json);

this avoids your having to define actual POJO to map data to.

Or you can use JsonNode (JSON Tree) as well.

Pretty-Print JSON in Java

Google's GSON can do this in a nice way:

Gson gson = new GsonBuilder().setPrettyPrinting().create();
JsonParser jp = new JsonParser();
JsonElement je = jp.parse(uglyJsonString);
String prettyJsonString = gson.toJson(je);

or since it is now recommended to use the static parse method from JsonParser you can also use this instead:

Gson gson = new GsonBuilder().setPrettyPrinting().create();
JsonElement je = JsonParser.parseString​(uglyJsonString);
String prettyJsonString = gson.toJson(je);

Here is the import statement:

import com.google.gson.*;

Here is the Gradle dependency:

implementation 'com.google.code.gson:gson:2.8.7'

Best way to make JSON pretty in Java

I think the best way beautify the json string is as follows using Jackson:

import com.fasterxml.jackson.databind.ObjectMapper;
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(yourObject)

Pretty printing JSON from Jackson 2.2's ObjectMapper

You can enable pretty-printing by setting the SerializationFeature.INDENT_OUTPUT on your ObjectMapper like so:

mapper.enable(SerializationFeature.INDENT_OUTPUT);

Does Jackson not pretty print values annotated by @JsonRawValue?

Maybe with the following code your test will pass :

Object json = mapper.readValue(input, Object.class);
String indented = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(json);

You can check that stackoverflow question and its own answers from which the code I have written upper was coming from, from the accepted answer :
Convert JSON String to Pretty Print JSON output using Jackson

How do I get the compact/minified form of pretty-printed JSON in Java?

Jackson allows you to read from a JSON string, so read the pretty-printed string back into Jackson and then output it again with pretty-print disabled.

See converting a String to JSON

Simple Example

    String prettyJsonString = "{ \"Hello\" : \"world\"}";
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readValue(prettyJsonString, JsonNode.class);
System.out.println(jsonNode.toString());

Requires

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.5.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.3</version>
</dependency>

How to get object writer with both pretty print and with a json view from objectmapper?

The easiest way is to enable SerializationFeature.INDENT_OUTPUT on ObjectMapper:

mapper.enable(SerializationFeature.INDENT_OUTPUT);

Or use withDefaultPrettyPrinter method:

mapper
.writerWithView(View.ConfigJson.class)
.withDefaultPrettyPrinter()
.writeValue(System.out, map);

You need to notice that writer* methods are declared in ObjectMapper and return ObjectWriter instance. Since that, you can use with* methods which are declared in ObjectWriter.



Related Topics



Leave a reply



Submit