Loading JSON to Java Converter...
Please wait a moment

JSON to Java: Classes for Jackson ObjectMapper

Paste a real API response and get Java classes whose field types match what is actually in it, long IDs included.

Step 1

Paste or upload your JSON

Drop in JSON from a REST API, a message queue or a config file. The more complete the sample, the better the classes: a field that never shows up in your sample can't show up in the code either.

Example input

{
  "product": {
    "id": 1001,
    "name": "Wireless Headphones",
    "price": 199.99,
    "inStock": true,
    "categories": ["Electronics", "Audio"],
    "manufacturer": {
      "name": "TechCorp",
      "country": "Japan"
    }
  }
}
Step 2

Check the types it picked

There are no settings on this page. The Java classes follow the data you paste, so a quick look at the types tells you whether the sample was representative.

•
Numbers: Integer when it fits in 32 bits, then Long, then BigInteger; anything with a decimal point or exponent is Double
•
Lists: every item is looked at, so fields that only appear in later items are kept
•
Text: quoted values stay String, so "00501" keeps its leading zeros
•
Mixed values: a field holding a number in one item and text in another becomes Object
Step 3

Read the generated classes

Everything lands in one file. Nested objects become public static classes inside Root, so the result compiles as a single Root.java.

Output for the example above (first part)

// Save as Root.java. Needs jackson-databind (com.fasterxml.jackson.core). Read it with:
//   Root data = new ObjectMapper().readValue(json, Root.class);
// For Gson, swap each @JsonProperty for @SerializedName and drop the @JsonAutoDetect lines.

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

@JsonAutoDetect(fieldVisibility = Visibility.ANY, getterVisibility = Visibility.NONE, isGetterVisibility = Visibility.NONE, setterVisibility = Visibility.NONE)
public class Root {

    @JsonProperty("product")
    private Product product;

    public Product getProduct() {
        return product;
    }

    public void setProduct(Product product) {
        this.product = product;
    }

    @JsonAutoDetect(fieldVisibility = Visibility.ANY, getterVisibility = Visibility.NONE, isGetterVisibility = Visibility.NONE, setterVisibility = Visibility.NONE)
    public static class Product {

        @JsonProperty("id")
        private Integer id;

        @JsonProperty("name")
        private String name;

        @JsonProperty("price")
        private Double price;

        @JsonProperty("inStock")
        private Boolean inStock;

        @JsonProperty("categories")
        private List<String> categories;

        @JsonProperty("manufacturer")
        private Manufacturer manufacturer;

        // ... getters, setters and the Manufacturer class follow
Step 4

Copy or download Root.java

Drop the file into your project next to the code that calls your REST API, add a package line if your project uses packages, and read the response.

•
Copy puts the whole file on your clipboard
•
Download saves it as Root.java, the name Java expects for the public Root class
•
Top-level lists: the comment at the top shows the TypeReference call for reading a JSON array
•
Check the data first: the JSON Validator points at the exact line of a syntax error

What is JSON to Java Conversion?

Reading JSON in Java usually means mapping it onto plain classes, with Jackson or Gson filling in the fields. Writing those classes by hand is slow and easy to get wrong, especially the field types and the names that don't follow Java conventions.

This page writes them for you from a real sample. If you want plain classes with no annotations at all, use the JSON to POJO page instead; to tidy the JSON first, try the JSON Formatter.

Frequently Asked Questions

Which library does the generated code need?

Only jackson-databind (Maven group com.fasterxml.jackson.core), which brings in jackson-annotations. Each field has @JsonProperty with the exact JSON key, and each class has a @JsonAutoDetect line that tells Jackson to read the fields directly, so the getters and setters are there for your own code and never confuse the mapping. Spring Boot already includes Jackson, so the classes work as request and response bodies there without extra setup.

Can I use the classes with Gson instead of Jackson?

Yes, with a small edit. Gson reads fields by their Java names, so replace each @JsonProperty("key") with @SerializedName("key") from com.google.gson.annotations and delete the @JsonAutoDetect lines and their imports. The comment at the top of the file says the same thing.

How does it choose between Integer, Long and Double?

It looks at how each number is written in your JSON, across every item of a list. Whole numbers that fit in 32 bits become Integer, bigger ones become Long (an ID like 8901240544102066246 keeps every digit), and anything past 64 bits becomes BigInteger. A number written with a decimal point or exponent, such as 14.50 or 1e3, becomes Double, or BigDecimal if the same field also holds whole numbers too long for a Double. Text in quotes, like "00501", stays a String so the leading zeros survive.

What happens when list items have different fields?

All items are merged into one class, so a field that appears only in some items is still there; when it is missing, Jackson leaves it null. If a field holds a whole number in one item and a decimal in another it becomes Double, and if it holds different kinds of values, say a number in one item and text in the next, it becomes Object.

How are odd JSON keys turned into Java names?

Keys are split into words and joined in camelCase, so "order_id" becomes orderId. Java keywords get Value added (class becomes classValue), keys that start with a digit get a field prefix, and names that collide, like "first-name" and "first_name", get a number. Class names that would hide String, List or Object get Model added. The @JsonProperty annotation keeps the original key, so reading the JSON still works.

Is my JSON sent anywhere?

No. The classes are generated in your browser as you type, and nothing you paste or upload is sent to our server or kept after you leave the page.