Help

JSON to POJO Converter: Java Classes or Records from JSON

Paste JSON and get ready-to-use Java: a class for every object, with private fields, a no-argument constructor and getters and setters, or Java records if you prefer. Nested objects are written inside their parent class, so the whole result pastes into one .java file. Add Jackson or Gson annotations when you need the original JSON keys.

How to convert JSON to a POJO

  1. Paste the JSON into the JSON box (it says Paste JSON here...). A paste is converted at once. When you type, the code updates about 300 milliseconds after you stop.
  2. Read the classes under Java code. Beneath it, a line such as Generated 2 classes. tells you how many were written.
  3. Select Copy, directly under the code. The button reads Copied for a moment. Save the code in a file named after the root class, for example Root.java.
  4. To change the result, expand Settings: set the Root class name (the default is Root), choose Annotations (None, Jackson or Gson) or tick Use records instead of classes. The code updates as soon as you change a setting.
  5. Select Clear to empty the box and start again.

Example: JSON in, Java classes out

This is what you paste:

JSON {
  "user_id": 7,
  "name": "Ana",
  "price": 4.5,
  "tags": ["a", "b"],
  "address": { "city": "Kyoto" }
}

With Annotations set to None, the result starts like this (the remaining getters and setters repeat the same pattern):

Java code import java.util.List;

public class Root {
    private Integer userId;
    private String name;
    private Double price;
    private List<String> tags;
    private Address address;

    public Root() {
    }

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    // getName/setName, getPrice/setPrice, getTags/setTags
    // and getAddress/setAddress follow the same pattern

    public static class Address {
        private String city;

        public Address() {
        }

        public String getCity() {
            return city;
        }

        public void setCity(String city) {
            this.city = city;
        }
    }
}

The nested address object became Address, a public static class inside Root, which is why one file is enough. The key user_id became the field userId. The status line adds 1 field name was changed to fit Java. to warn you about it.

The same JSON as a record with Jackson annotations

Set Root class name to Order, Annotations to Jackson and tick Use records instead of classes:

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

public record Order(
    @JsonProperty("user_id") Integer userId,
    @JsonProperty("name") String name,
    @JsonProperty("price") Double price,
    @JsonProperty("tags") List<String> tags,
    @JsonProperty("address") Address address
) {
    public record Address(
        @JsonProperty("city") String city
    ) {}
}

Every record component carries @JsonProperty("key") with the original JSON key, so user_id still maps to userId. Choose Gson and the same components carry @SerializedName("key") instead, with the Gson import. Records need Java 16 or newer.

How JSON types become Java types

All types are boxed, so a missing or null value is allowed in every field.

In the JSONExampleJava type
Whole number7Integer
Whole number beyond the Integer range12345678901Long
Whole number beyond the Long range99999999999999999999BigInteger (with its import)
Number with a decimal point or exponent4.5, 1.0, 2e3Double
Text"Ana"String
True or falsetrueBoolean (getter getActive, not isActive)
NullnullObject
Array of one type["a", "b"]List<String> (with java.util.List imported)
Empty array, or mixed types[], [1, "a"]List<Object>
Array inside an array[[1, 2]]List<List<Integer>>
Object{"city": "Kyoto"}a nested class or record named after the key

Numbers are read exactly as written, so a long ID is never rounded. Imports are added only when the code uses them. If a key appears twice in the same object, the last one wins.

How class and field names are chosen

JSON keyBecomesWhy
user_id, user-namefield userId, userNamecamelCase for fields
classfield _classa Java keyword gets an underscore
2ndfield _2nda name cannot start with a digit
items holding an array of objectsclass Item, field List<Item>the array key is singularised
String holding an objectclass StringDatanames that would shadow String, List or Object get Data added
Two objects named alikea number is added to the secondclass names must be unique in the file

The root class name is cleaned into a valid Java name, and an empty Root class name box gives Root. When the JSON itself starts with an array of objects, Root describes one item, and the page says Read the JSON as List<Root>.

When the JSON is not accepted

The reader is strict, as JSON is: comments, single quotes and trailing commas are errors. The Could not read this JSON box gives a plain reason and the position as Line N, column M, for example The parser stopped at “}”. Check for a missing comma, colon, bracket or quote just before it. or The text ends too early. A bracket, a brace or a quote is still open.

Valid JSON that holds no object, such as 42 or an array of numbers, shows There is no object in this JSON to turn into a class. Paste an object, or an array of objects. Text over 2,000,000 characters is refused, and so is nesting deeper than 200 levels.

Frequently asked questions

How do I convert JSON to a POJO in Java?

Paste the JSON into the JSON box and copy the code from Java code. You get one class per JSON object, with private fields, a public no-argument constructor and public getX and setX methods. Save the code in a file named after the root class, for example Root.java, then read the JSON with Jackson or Gson.

How do I convert a JSON array to Java objects?

Paste the array of objects as it is. The tool describes one item of the array as the root class and tells you to read the JSON as List<Root>. In Jackson that is a TypeReference<List<Root>>, in Gson a TypeToken<List<Root>>. An array nested inside an object becomes a List<Item> field, with the class named after the key.

How do I keep the original JSON keys with Jackson or Gson?

Open Settings and choose Jackson or Gson under Annotations. Jackson adds @JsonProperty("key") and Gson adds @SerializedName("key") on every field, plus the import. With None, a key such as user_id becomes the field userId, which will not map back to the JSON unless the original key is kept by an annotation or a naming strategy.

Can I generate Java records from JSON?

Yes. Tick Use records instead of classes under Settings. Each JSON object becomes public record Name(Type a, Type b) {}, and nested objects become records inside the parent record. Records need Java 16 or newer. Annotations still work and are placed on each record component.

Why is a JSON number an Integer in one place and a Double in another?

The type follows how the number is written. A whole number is an Integer, or a Long when it is too big for an Integer, or a BigInteger when it is too big for a Long. A number written with a decimal point or an exponent is a Double, even when it is 1.0.

What happens when the objects in an array have different keys?

They are merged into one class that holds every key seen in any of them, so keys that only some objects have still get a field. If a key holds different types, or an array is empty, the field is typed Object or List<Object>. Check those fields by hand.

Does it generate Kotlin classes or Lombok annotations?

No. The output is plain Java: classes with getters and setters, or records. If you use Lombok, delete the getter and setter methods and put @Data on the class. For Kotlin, a data class needs to be written separately.

How large can the JSON be, and is it uploaded?

Up to 2,000,000 characters, nested up to 200 levels deep. The conversion runs in your browser, so the JSON is not uploaded or saved anywhere.

Recommended

Helps keep the tools free. ✨

Images are for illustration only. As an Amazon Associate I earn from qualifying purchases.