Converting Java objects to JSON with Jackson
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Jackson is the default JSON library in a large part of the Java ecosystem because it handles ordinary Java objects with very little ceremony. In the simplest case, you create an ObjectMapper, call writeValueAsString, and get JSON back.
The more interesting part is controlling output shape, dates, null handling, and nested objects without turning serialization into custom boilerplate. That is where a good Jackson setup matters.
Start With ObjectMapper
The central class is ObjectMapper. It knows how to inspect fields or getters, serialize collections, and write JSON to strings, streams, or files.
Here is a complete example:
Output:
Jackson can produce this because the class exposes readable properties through getters. Public fields also work, but normal getter-based domain objects are usually cleaner.
Serialize Collections and Nested Types
Jackson handles nested objects and lists naturally, which is why it fits REST APIs so well.
This gives a JSON array with nested objects, without any manual string assembly.
Control Names and Null Handling
Real APIs often need field names that differ from Java names, or they want to skip null values. Jackson annotations cover both cases cleanly.
With that definition, Jackson emits full_name and omits age when it is null. This is usually better than post-processing JSON text after serialization.
Pretty Printing and Reusable Configuration
For debugging, fixtures, and local tools, pretty printing makes output easier to read:
In production code, do not create a new mapper in every method. Build one configured mapper and reuse it. That keeps behavior consistent across the application, especially for:
- Java time serialization
- naming strategy
- null inclusion rules
- custom modules
- pretty-print behavior in specific environments
Frameworks such as Spring Boot usually manage this for you, but the principle is the same even in plain Java.
Writing to Files or Streams
You do not always need an intermediate String. Jackson can write directly to a destination:
This is often the right choice for exports, test fixtures, or CLI tools because it avoids one more conversion step.
Common Pitfalls
One common mistake is thinking toString() affects Jackson output. It does not. Jackson serializes the object's properties, not its string representation.
Another problem is scattering many local ObjectMapper instances through the codebase. That produces subtle differences in JSON shape, date formatting, and null handling that are hard to track down later.
Developers also run into visibility issues. If a class has no visible fields, no getters, or uses constructors in a way Jackson cannot infer, the output may be empty or fail unexpectedly. When that happens, inspect the class design first instead of blaming JSON generation broadly.
Finally, avoid manual string concatenation to build JSON. It is brittle, escapes poorly, and becomes a maintenance problem as soon as the object shape grows.
Summary
- Jackson uses
ObjectMapperto convert Java objects into JSON. - '
writeValueAsStringis the simplest way to produce a JSON string.' - Collections and nested objects serialize naturally when the object model is clear.
- Use annotations such as
@JsonPropertyand@JsonIncludeto control output shape. - Reuse a configured
ObjectMapperinstead of creating ad hoc mappers throughout the application.

