Pretty printing JSON from Jackson 2.2's ObjectMapper
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Pretty-printing JSON with Jackson means serializing it with indentation and line breaks so humans can read it comfortably. The standard answer is to use ObjectMapper.writerWithDefaultPrettyPrinter(), and that approach works both when you start from a Java object and when you parse raw JSON into a tree before reformatting it.
Pretty Print a Java Object
If you already have a Java object, the easiest path is to serialize it with the default pretty printer.
This is the most common use case: take an object and emit readable JSON for logging, debugging, or configuration output.
Pretty Print an Existing JSON String
If the input is already JSON text, parse it first and then write it back with pretty printing.
This is safer than trying to insert line breaks manually because the JSON parser understands the structure correctly.
Why readTree Is Useful
When the source is a raw JSON string, parsing it into a JsonNode first gives you two benefits:
- invalid JSON fails early with a real parse error
- the pretty printer formats structural JSON, not arbitrary text
That makes the pipeline more robust than working only with string replacement tricks.
Writing Pretty JSON to a File
Pretty printing is often useful when generating files meant for humans to inspect.
This is common for local config generation, snapshots, fixtures, or debugging dumps.
Pretty Printing Is for Readability, Not Efficiency
Pretty-printed JSON is larger than compact JSON because it includes whitespace. That is usually fine for logs, config files, or developer-facing diagnostics, but it is often a poor choice for network payloads where size matters.
So the general rule is:
- pretty print for humans
- compact print for machines or bandwidth-sensitive paths
Jackson supports both easily, which is why the decision is usually about use case rather than capability.
Custom Pretty Printers Exist
If the default layout is not enough, Jackson also lets you provide a custom pretty printer. Most projects never need this, but it is available when you want stricter formatting control.
In practice, writerWithDefaultPrettyPrinter() is the right first choice unless you have a very specific formatting policy.
Common Pitfalls
The biggest mistake is trying to pretty print JSON by hand with string replacements. That breaks as soon as nested objects, arrays, or escaped characters become involved.
Another issue is assuming pretty printing repairs malformed JSON. It does not. Invalid JSON must still parse successfully before Jackson can reformat it.
Developers also sometimes pretty print large high-throughput API responses unnecessarily, which increases payload size for no operational benefit.
Finally, if you are comparing pretty-printed output in tests, remember that formatting style matters. Test semantic content when possible rather than depending too tightly on exact whitespace layout.
Summary
- Use
writerWithDefaultPrettyPrinter()to pretty print JSON with Jackson. - Serialize Java objects directly or parse raw JSON into a
JsonNodefirst. - Pretty printing improves readability, not performance.
- Do not try to format structured JSON with manual string hacks.
- Reserve pretty output for human-facing uses such as logs, fixtures, and config files.
Related reading
- Prevent Application / CommandLineRunner classes from executing during JUnit testing
- Prevent @RabbitListener in spring-rabbit from trying to connect to server during integration test
- Prevent Spring Boot application closing until all current requests are finished
- Prevent Spring Boot from registering a servlet filter
- Prevent stack trace logging for custom exception in Spring Boot application
- Print all the Spring beans that are loaded - Spring Boot
- Print an integer in binary format in Java
- Print the data in ResultSet along with column names

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.