Z

POJO to JSON Converter — Serialise Java Objects with Jackson, Gson & Moshi

Convert a Java POJO (Plain Old Java Object) instance into JSON using Jackson, Gson, or Moshi. Copy-paste-ready code recipes plus the reverse converter when you need to scaffold the POJO itself.

Free No signup Client-side Privacy friendly Updated
Heads up: POJO → JSON conversion needs the running JVM (so the serialiser can read actual field values). The recipes below run in your own app; this page exists as a quick reference. Looking for the reverse — JSON to a Java POJO class? Use JSON to Java POJO.

Jackson — the default in Spring

Spring Boot ships Jackson by default. ObjectMapper is thread-safe — reuse a single instance.

import com.fasterxml.jackson.databind.ObjectMapper;

private static final ObjectMapper MAPPER = new ObjectMapper();

public static String toJson(Object pojo) throws JsonProcessingException {
    return MAPPER.writeValueAsString(pojo);
}

// Pretty-printed:
String pretty = MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(pojo);

Annotate fields with @JsonProperty("snake_case") when the JSON field name differs from the Java field. @JsonIgnore skips a field; @JsonInclude(NON_NULL) drops nulls.

Gson — minimal dependency, Android friendly

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

private static final Gson GSON = new GsonBuilder()
    .serializeNulls()              // include null fields
    .setPrettyPrinting()           // optional
    .create();

public static String toJson(Object pojo) {
    return GSON.toJson(pojo);
}

Use @SerializedName("json_field") on a Java field to override the JSON key. Mark fields transient to skip them.

Moshi — Kotlin-first, codegen option

import com.squareup.moshi.Moshi;
import com.squareup.moshi.JsonAdapter;

Moshi moshi = new Moshi.Builder().build();
JsonAdapter<User> adapter = moshi.adapter(User.class);

String json = adapter.toJson(user);
// Pretty:
String pretty = adapter.indent("  ").toJson(user);

How POJO to JSON compares to alternatives

POJO to JSON versus typical browser and desktop alternatives — features compared at a glance.
Concern Jackson Gson Moshi
Default in Spring Boot Jackson Jackson
Smallest jar Gson (~280KB) Jackson (3 modules ~2MB)
Kotlin support Moshi (best) Jackson kotlin module Gson (kludgy)
Annotation-free path Jackson + naming strategy Gson + FieldNamingPolicy Moshi reflection adapter
Streaming API JsonGenerator JsonWriter JsonWriter

Frequently asked questions

Which library should I use to convert a POJO to JSON?

For Spring Boot use Jackson — it's bundled and the rest of the stack expects it. For Android or low-dependency CLIs, pick Gson (smaller jar) or Moshi (better Kotlin). All three handle the same POJO; the choice is about ecosystem fit, not capability.

How do I rename a field in the JSON output?

Jackson: <code class="font-mono">@JsonProperty("name")</code>. Gson: <code class="font-mono">@SerializedName("name")</code>. Moshi: <code class="font-mono">@Json(name = "name")</code>. All three annotations sit on the field declaration.

How do I skip null or empty fields?

Jackson: <code class="font-mono">@JsonInclude(JsonInclude.Include.NON_NULL)</code> on the class or field. Gson: nulls are excluded by default — call <code class="font-mono">.serializeNulls()</code> on the builder to include them. Moshi: nulls are excluded unless the adapter is configured with <code class="font-mono">.serializeNulls()</code>.

Why doesn't my POJO serialise — I get an empty {} object?

Jackson needs either public getters or a default constructor plus accessible fields. With Lombok, make sure <code class="font-mono">@Getter</code> (or <code class="font-mono">@Data</code>) is applied. With records (Java 16+), accessors are auto-generated.

Can I do POJO-to-JSON without writing Java code?

Not exactly — JSON serialisation depends on the actual values held in a Java instance at runtime, so you need the JVM. What you can do without code: scaffold the POJO from a JSON sample using our <a class="underline" href="https://tools.zerethon.com/json-to-java">JSON to Java POJO</a> generator, then drop the recipes above into your project.

Is the conversion lossy?

Not for primitives and Strings. Some Java types need adapters: <code class="font-mono">LocalDateTime</code> (Jackson: register <code class="font-mono">JavaTimeModule</code>), <code class="font-mono">BigDecimal</code> precision (Jackson: <code class="font-mono">USE_BIG_DECIMAL_FOR_FLOATS</code>), and <code class="font-mono">enum</code> values (configurable as <code class="font-mono">name()</code> or <code class="font-mono">ordinal()</code>).

Need the reverse?

Paste a JSON sample and get a typed Java POJO class — Jackson, Lombok, or record style, with nested classes auto-generated.

Open JSON to Java POJO →

What is POJO to JSON?

POJO to JSON conversion takes a Java instance — a Plain Old Java Object with fields, getters, and setters — and serialises it into a JSON string. The standard libraries are Jackson (`ObjectMapper.writeValueAsString(obj)`), Gson (`new Gson().toJson(obj)`), and Moshi (`moshi.adapter(MyType.class).toJson(obj)`). Each library reflects over the object's fields, applies any annotations (e.g. `@JsonProperty`, `@SerializedName`), and emits valid JSON.

Summary

POJO to JSON is a free developer utility by Zerethon Tools. Convert a Java POJO (Plain Old Java Object) instance into JSON using Jackson, Gson, or Moshi. Copy-paste-ready code recipes plus the reverse converter when you need to scaffold the POJO itself. Runs entirely in the browser — no signup, no upload.

Category
Developer
Pricing
Free
Privacy
Browser-based
Signup
Not required

Privacy

Your data never leaves your browser unless explicitly stated. POJO to JSON runs entirely client-side — no server upload, no logging, no tracking of your input.

Related tools

Build, share, and grow on Zerethon Social

Free signup. Earn points, collect achievements, and connect with creators worldwide.

Try Zerethon free