JSON to Java

Generate Java records or POJOs with Jackson annotations from JSON.

Runs in your browser Instant No sign-up
Style
JSON input
Java

Paste, type or drop a file — output appears as you type.

Waiting for input
Processed locally · nothing is uploadedProcessed locally

How it works

Paste a JSON sample (an API response, a webhook payload, a config file) and get Java types for Jackson:

  1. Every object becomes a record or class named after its property: customer → Customer, and array items use the singular (addresses → Address). The root is Root unless you rename it.
  2. Nested types are emitted as nested records or static classes inside the root type, so the output is one compilable Root.java file.
  3. Arrays of objects are merged: a key that appears in only some items is still captured, and its type becomes boxed so it can be null.
  4. Numbers become int, long or BigInteger depending on the largest value seen, or double when any value has a fraction. Arrays become List<T>, and empty objects become Map<String, Object>.
  5. Imports (java.util.List, java.util.Map, java.math.BigInteger, com.fasterxml.jackson.annotation.JsonProperty) are added only when used.

Example

{
  "user_id": 42,
  "name": "Linh",
  "tags": ["admin"],
  "addresses": [
    { "city": "Hanoi", "zip": "100000", "note": null },
    { "city": "HCMC", "zip": "700000", "primary": true, "note": "Gate 2" }
  ]
}

becomes

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

public record Root(
    @JsonProperty("user_id") int userId,
    String name,
    List<String> tags,
    List<Address> addresses
) {
    public record Address(
        String city,
        String zip,
        String note,
        Boolean primary
    ) {}
}

primary is missing from the first address, so it becomes Boolean rather than boolean. note is a String, which can already hold null.

Options

  • Root name: name of the top-level type.
  • Style: Record (Java 16+) or POJO with getters/setters. POJOs get private fields, getX()/setX() accessors (isX() for primitive boolean) and public static nested classes.
  • @JsonProperty on all: annotate every field, not just renamed ones.
  • Package (under More options): prepends a package declaration.

Reserved words and odd keys

Keys that are not valid Java names are converted and the original key is kept in @JsonProperty:

public record Root(
    @JsonProperty("class") String clazz,
    @JsonProperty("default") Object default_,
    @JsonProperty("2fa") boolean field2fa,
    @JsonProperty("first name") String firstName,
    @JsonProperty("created_at") long createdAt,
    @JsonProperty("balance_minor") BigInteger balanceMinor
) {}

Types that would shadow ones the code uses (String, List, Map, Object and so on) get a Type suffix, so a "string": {…} key gives a nested StringType.

Reading JSON with the generated types

ObjectMapper mapper = new ObjectMapper();
Root root = mapper.readValue(json, Root.class);
// Root array: List<RootItem> items = mapper.readValue(json, new TypeReference<List<RootItem>>() {});

When the JSON root is an array or a primitive, the output starts with a comment such as // JSON root type: List<RootItem>, because Java has no type aliases.

Common mistakes

  • Generating from a single object. Every key becomes a required primitive. Paste an array of several real responses so optional fields are detected.
  • Unknown properties. By default Jackson fails on keys that the class does not declare (UnrecognizedPropertyException). Add @JsonIgnoreProperties(ignoreUnknown = true) or disable FAIL_ON_UNKNOWN_PROPERTIES if the API may add fields.
  • Whole-number prices. A price of 25 in the sample becomes int. Use a sample with decimals, or change it to BigDecimal for money.
  • Fields typed Object. All sample values were null, the array was empty, or the values mixed types. Replace Object with the real type.

Generating the same payload for other stacks? Try JSON to Kotlin, JSON to C# or JSON to TypeScript, and fix broken samples first with JSON Repair.

Frequently asked questions

Should I pick records or POJOs?

Records (Java 16+) are shorter and immutable, and Jackson 2.12+ deserializes them through the canonical constructor. Pick POJOs when you are on Java 8/11, need mutable objects, or use a framework that requires a no-argument constructor and setters (older JPA or JAXB setups, some form binders).

When is @JsonProperty added?

Whenever the Java name differs from the JSON key: snake_case and kebab-case keys (`user_id` → `userId`), keys with spaces or a leading digit, and reserved words (`class` → `clazz`, `default` → `default_`). Turn on "@JsonProperty on all" to annotate every field, which keeps the mapping explicit even if a naming strategy is configured on the ObjectMapper.

Why is a field `int` in one class and `Integer` in another?

Primitives are used only when every sample object had the key and the value was never null. A key missing from some array items, or a null value, makes the field boxed (`Integer`, `Long`, `Double`, `Boolean`) so the absence can be represented as `null`.

How are big numbers typed?

From the range of the sample values: `int` up to 2,147,483,647, `long` up to 2^63−1, and `java.math.BigInteger` beyond that. Big integers are read losslessly, so an ID like 9007199254740993 keeps every digit when choosing the type.

Is my JSON uploaded?

No. The classes are generated entirely in your browser; nothing is sent to a server.

Last updated