Explain Codes LogoExplain Codes Logo

Convert JSON String to Pretty Print JSON Output Using Jackson

java
json
pretty-print
objectmapper
Nikita BarsukovbyNikita Barsukov·Aug 12, 2024
TLDR
ObjectMapper mapper = new ObjectMapper(); String prettyJson = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonNode);

Transforming JSON to Pretty Print: Get Jackson's ObjectMapper, initiate writerWithDefaultPrettyPrinter() for neat output, and employ writeValueAsString(jsonNode) to reveal a beautified JSON string.

Getting started: JSON objects and Pretty printing

From JSON to Java objects

Jackson shines at binding JSON data to Java objects. Turn this magic on with a simple call:

ObjectMapper mapper = new ObjectMapper(); // Spoiler: JSON turns into a POJO here! Object jsonObj = mapper.readValue(jsonString, Object.class);

Here, readValue() is our magic wand that deserializes the JSON content into a Java Object.

Pretty print your JSON

Appreciate organized, easy-to-read JSON outputs? Jackson has got you covered with its pretty print feature:

// Let's tidy up this JSON! ObjectMapper mapper = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT); String prettyJson = mapper.writeValueAsString(jsonObj);

Now, ObjectMapper will happily indent your JSON data due to the magic of .enable(SerializationFeature.INDENT_OUTPUT).

Flexibility with JsonNode

Need more control? Try working with JsonNode for an adjustable ride:

// JSON, you can't hide in the tree anymore! JsonNode jsonNode = mapper.readTree(jsonString); String prettyJson = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonNode);

The JsonNode provides a more dynamic structure - perfect for making adjustments before whipping out that final pretty print!

Moving forward: Debugging and version compatibility

Debugging discrepancies

Seeing unexpected results? It's debugging time! Compare the JSON string, POJO and model display. Spot any differences? They're likely pointing to serialization issues, errors with your POJO structure, or data inconsistencies.

Jackson version check

Check your Jackson versions! Always ensure you're using Jackson 1.9+ for hassle-free pretty print. Older versions may require a sneaky configuration of SerializationConfig.Feature.INDENT_OUTPUT.

Common pitfalls and solutions

  • Confirm your POJO layout and JSON are partners in crime.
  • Review your ObjectMapper configurations for a smooth sailing.
  • Consult the official Jackson docs when in doubt — they're like a labyrinth full of treasures.

Inside Jackson: Reusability, custom indentation and handling large files

Unlock ObjectMapper's potential

Reuse, don't duplicate! ObjectMapper instances are thread-safe and can be reused for improved performance.

Custom indentation Preferences

Not a fan of standard indentation? No worries, create a custom DefaultPrettyPrinter and configure to your heart's desire.

Major league JSON files

Working with large JSON data? Try using JsonParser and JsonGenerator for streaming - a much more memory-efficient solution.

Bonus: Third-party modules

Jackson's plugin-style modules are here to save the day. Handle a variety of data formats other than JSON like XML, CSV, or Avro.