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
- 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.
- Read the classes under Java code. Beneath it, a line such as Generated 2 classes. tells you how many were written.
- 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. - 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.
- Select Clear to empty the box and start again.
Example: JSON in, Java classes out
This is what you paste:
{ "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):
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:
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 JSON | Example | Java type |
|---|---|---|
| Whole number | 7 | Integer |
| Whole number beyond the Integer range | 12345678901 | Long |
| Whole number beyond the Long range | 99999999999999999999 | BigInteger (with its import) |
| Number with a decimal point or exponent | 4.5, 1.0, 2e3 | Double |
| Text | "Ana" | String |
| True or false | true | Boolean (getter getActive, not isActive) |
| Null | null | Object |
| 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 key | Becomes | Why |
|---|---|---|
user_id, user-name | field userId, userName | camelCase for fields |
class | field _class | a Java keyword gets an underscore |
2nd | field _2nd | a name cannot start with a digit |
items holding an array of objects | class Item, field List<Item> | the array key is singularised |
String holding an object | class StringData | names that would shadow String, List or Object get Data added |
| Two objects named alike | a number is added to the second | class 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.
- If your text is a JavaScript object with unquoted keys or single quotes, run it through the JSON Stringifier first to get strict JSON.
- To reformat or tidy the JSON before converting it, use the JSON Formatter.
- To check the JSON against a schema, use the JSON Schema Validator.
- For TypeScript interfaces from JavaScript, see the JS to TS Converter.
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
-
Lamicall 360° Rotating Tablet StandStand your Android test phone beside the laptop to watch the new model classes parse the payload.
-
Soundcore Q30 Noise Cancelling HeadphonesShut out the office while you trace a long JSON payload into model classes.
-
Elgato Stream Deck Neo (8 keys)Put build, run and test on one key so every new endpoint is a single press away.
Helps keep the tools free. ✨
Images are for illustration only. As an Amazon Associate I earn from qualifying purchases.