Skip to content

JSON to Java

Paste a JSON sample, such as an API response, to generate Java classes for it. Choose plain POJOs, Lombok or records. Nested objects become their own classes and odd key names get @JsonProperty.

JSON
Java

    
Ready

Last reviewed:

What is JSON to Java conversion?

Java applications that call REST APIs or read JSON files usually map the JSON onto typed classes rather than working with raw maps. A library such as Jackson (the default in Spring Boot) or Gson reads {"first_name": "Ada"} and fills in a firstName field on a Customer object, so the rest of your code gets compile-time checking, IDE completion and clear types. Writing those classes by hand for a large response, with nested objects, lists and dozens of fields, is slow and error-prone.

A JSON-to-Java generator infers the classes from a sample document. Strings become String, whole numbers become Integer (or Long when they exceed the 32-bit range), decimals become Double, true/false become Boolean, arrays become List<…>, and each nested object becomes its own class named after its key. Keys that are not valid or conventional Java names, such as first_name, class or 2fa, are converted to camelCase names and annotated with @JsonProperty so Jackson still maps them to the right JSON key.

There are three common styles for the result. POJOs are plain classes with private fields, getters and setters, and work everywhere. Lombok classes use @Data to generate the accessors at compile time, keeping the source short. Records, available since Java 16, are immutable and concise, and Jackson supports them from version 2.12.

How to use this tool

  1. Paste a JSON sample into the left box, or click Paste. Sample loads a customer with nested address and orders.
  2. Set the Class name for the root object (default Customer) and, optionally, a Package.
  3. Choose POJO, Lombok or Record.
  4. Choose how Jackson annotations are added: @JsonProperty when needed (only when the Java name differs from the JSON key), @JsonProperty on every field, or No annotations.
  5. Tick Primitive types to use int, long, double and boolean instead of their wrapper classes. List elements always stay boxed, because generics cannot hold primitives.
  6. Tick Separate files to output each class as its own top-level class, marked with a // Name.java comment, instead of nested static classes.
  7. Click Copy, or Download to save <Class>.java. The status line tells you if the JSON is invalid, or if the root was an array and you should read it as List<…>.

Examples

Snake case keys to a POJO

Input:

{ "id": 7, "display_name": "Ada", "active": true, "score": 9.5 }

Output (POJO, annotations when needed; getters and setters for the other fields are generated the same way):

import com.fasterxml.jackson.annotation.JsonProperty;

public class Customer {
    private Integer id;
    @JsonProperty("display_name")
    private String displayName;
    private Boolean active;
    private Double score;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }
}

The same input as a Record with package com.example.model:

package com.example.model;

import com.fasterxml.jackson.annotation.JsonProperty;

public record Customer(
    Integer id,
    @JsonProperty("display_name") String displayName,
    Boolean active,
    Double score
) {
}

Nested lists, nulls and awkward keys (Lombok)

Input:

{
  "orderId": "ORD-1",
  "items": [{ "sku": "KB-01", "qty": 1 }, { "sku": "MS-02", "qty": 2, "price": 19.99 }],
  "tags": [],
  "couponCode": null,
  "createdAt": "2026-10-08T09:30:00Z",
  "class": "gold",
  "2fa": false,
  "total": 3000000000
}

Output with class name Order:

import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.List;
import lombok.Data;

@Data
public class Order {
    private String orderId;
    private List<Item> items;
    private List<Object> tags;
    private Object couponCode;
    private String createdAt;
    @JsonProperty("class")
    private String class_;
    @JsonProperty("2fa")
    private Boolean _2fa;
    private Long total;

    @Data
    public static class Item {
        private String sku;
        private Integer qty;
        private Double price;
    }
}

The two array elements are merged, so Item has price even though only the second item had it. items is singularised to the class name Item. class is a reserved word and 2fa starts with a digit, so both get safe names plus @JsonProperty. 3000000000 is beyond Integer.MAX_VALUE (2,147,483,647), so total is a Long.

Common errors and how to fix them

  • "Invalid JSON" such as "Expected property name or '}' in JSON at position 1". The sample is not strict JSON, often single quotes or a trailing comma copied from JavaScript. Fix it in the JSON formatter first.
  • "The JSON must be an object or an array of objects." A root like [1, 2] or "text" has no fields to turn into a class. Wrap it in an object or generate from a single element.
  • Object or List<Object> fields. The sample had null or an empty array there, so the type could not be inferred. Paste a sample where the field has a value, or edit the type by hand.
  • A field typed Integer that sometimes has decimals. The sample only contained whole numbers. Change it to Double or BigDecimal, especially for money, or include a decimal value in the sample.
  • Jackson fails with "Unrecognized field". The real JSON has a field your sample did not. Regenerate with a fuller sample, or add @JsonIgnoreProperties(ignoreUnknown = true) to the class.
  • Records fail to deserialise on old Jackson. Records need Jackson 2.12 or newer. Upgrade, or use the POJO or Lombok style.

When developers use this

  • Creating DTOs for a third-party REST API from a response captured with cURL or the browser's network tab.
  • Modelling a webhook payload, such as a payment or Git hosting event, before writing the handler.
  • Writing request and response classes for integration tests from recorded fixtures.
  • Turning a JSON configuration file into typed settings classes.
  • Moving from Map<String, Object> parsing to typed classes in an existing codebase.

Privacy and how it works

The JSON is parsed and the classes are generated in your browser by a small JavaScript type-inference routine. Nothing is uploaded. Real API responses often contain personal data, tokens and internal IDs, so it is safer that the sample never leaves your machine. Even so, consider replacing sensitive values before sharing generated code, because samples sometimes end up in comments or tests.

Limitations

  • Types are inferred from one sample only. Optional fields, nullability and value ranges cannot be known from data that does not show them.
  • Date and time strings, such as createdAt, stay String. Change them to Instant, OffsetDateTime or LocalDate and configure Jackson's JavaTimeModule if you want real date types.
  • Enums, validation annotations, equals/hashCode for POJOs, and builders are not generated.
  • Objects with exactly the same structure share one class. Objects that differ, even slightly, get separate classes with numbered names.

FAQ

Should I choose POJO, Lombok or Record?

Use records for immutable data on Java 16 or newer; they are the most concise and need no extra library. Use Lombok if your project already depends on it and you need mutable classes. Use POJOs when you cannot add dependencies or need maximum compatibility.

Why are wrapper types like Integer used by default?

Wrapper types can hold null, which is how a missing or null JSON field is represented. A primitive int would silently become 0, which you cannot tell apart from a real zero. Tick Primitive types only for fields that are always present.

Does it work with Gson instead of Jackson?

The classes work with Gson, but @JsonProperty is a Jackson annotation. Choose No annotations and add Gson's @SerializedName where a name differs, or configure Gson's field naming policy for snake case.

How are nested class names chosen?

From the JSON key, converted to PascalCase. Array keys are singularised: orders becomes Order and categories becomes Category. If a name is already taken by a different structure, a number is added, such as Address2.

What happens when the root JSON is an array?

All elements are merged into one class, named from the Class field, and the status line reminds you to read the JSON as List<YourClass>, for example with new TypeReference<List<Row>>() {} in Jackson.

Why is my number field a Long?

At least one sample value was larger than the 32-bit Integer range (±2,147,483,647), which is common for IDs and timestamps in milliseconds. Long holds values up to about 9.2 × 10^18.

Is it safe to paste a production API response?

The data stays in your browser and is not sent anywhere. Still, remove secrets and personal data from samples you later commit as test fixtures.

Guides