JSON to Java
Paste a JSON sample. Get Java types, and the code that reads and writes the JSON. It runs in your browser, and your data never leaves it.
Open the tool with Java chosen
An example
This JSON:
{
"id": 42,
"name": "Ada Lovelace",
"active": true,
"joined": "2020-01-02T03:04:05Z",
"tags": ["engineer", "writer"],
"address": { "city": "London", "postcode": null },
"scores": [9.5, 8, 10]
}
becomes this Java:
// Root.java
package io.quicktype;
import java.time.OffsetDateTime;
import java.util.List;
public class Root {
private long id;
private String name;
private boolean active;
private OffsetDateTime joined;
private List<String> tags;
private Address address;
private List<Double> scores;
public long getID() { return id; }
public void setID(long value) { this.id = value; }
public String getName() { return name; }
public void setName(String value) { this.name = value; }
public boolean getActive() { return active; }
public void setActive(boolean value) { this.active = value; }
public OffsetDateTime getJoined() { return joined; }
public void setJoined(OffsetDateTime value) { this.joined = value; }
public List<String> getTags() { return tags; }
public void setTags(List<String> value) { this.tags = value; }
public Address getAddress() { return address; }
public void setAddress(Address value) { this.address = value; }
public List<Double> getScores() { return scores; }
public void setScores(List<Double> value) { this.scores = value; }
}
// Address.java
package io.quicktype;
public class Address {
private String city;
private Object postcode;
public String getCity() { return city; }
public void setCity(String value) { this.city = value; }
public Object getPostcode() { return postcode; }
and 3 more lines. Open the tool to see the whole file.
With the reader and the writer
The tool also writes the code that turns the text into those types, and back again. That code checks the shape as it reads, which is the part a type alone cannot do.
// Converter.java
// To use this code, add the following Maven dependency to your project:
//
//
// com.fasterxml.jackson.core : jackson-databind : 2.9.0
// com.fasterxml.jackson.datatype : jackson-datatype-jsr310 : 2.9.0
//
// Import this package:
//
// import io.quicktype.Converter;
//
// Then you can deserialize a JSON string with
//
// Root data = Converter.fromJsonString(jsonString);
package io.quicktype;
import java.io.IOException;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import java.util.*;
import java.time.LocalDate;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;
public class Converter {
// Date-time helpers
private static final DateTimeFormatter DATE_TIME_FORMATTER = new DateTimeFormatterBuilder()
.appendOptional(DateTimeFormatter.ISO_DATE_TIME)
.appendOptional(DateTimeFormatter.ISO_OFFSET_DATE_TIME)
.appendOptional(DateTimeFormatter.ISO_INSTANT)
.appendOptional(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SX"))
.appendOptional(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ssX"))
.appendOptional(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))
.toFormatter()
.withZone(ZoneOffset.UTC);
and 133 more lines. Open the tool to see the whole file.
What you need in your project
Jackson, `com.fasterxml.jackson.core:jackson-databind`.
The code uses Jackson, which is the reader most Java projects already have. The comment at the top of the file holds the Maven line to add.
"Types only" gives the classes with no reader, for a project that already has its own.
A JSON key that is not a legal Java name is renamed, and a `@JsonProperty` keeps the original.
The trap to know about
A JSON number with no decimal point becomes `long`. If your API ever sends a decimal in that field, the read fails. A JSON Schema input fixes this, because it names the type.
The options you can change
These belong to Java, so the row of switches changes with the language.
| Option | What it does | Default |
|---|---|---|
| Array type | Use T[] or List<T> | array | list |
| Types only | Plain types only | false |
| Date time provider type | Date time provider type | java8 | legacy |
| Package name | Generated package name | io.quicktype |
| Lombok | Use lombok | false |
Questions
How do I turn JSON into Java?
Paste your JSON into the box, choose Java, and the code appears at once. Give the top type a name, and every other name follows from your keys. Then copy the code, or download it as a file.
Does my JSON go to a server?
No. The generator runs inside your browser, in a worker. Nothing is uploaded, nothing is stored, and the tool works with the network turned off.
What do I need to add to my Java project?
Jackson, `com.fasterxml.jackson.core:jackson-databind`.
How does it know which fields are optional?
From your sample. A key that is missing, or holds null, becomes optional. One sample cannot show everything, so give more than one, or start from a JSON Schema.
Other languages
Try it on your own JSON. The generator runs in your browser. Nothing is uploaded, and nothing is stored on a server.
Open JSON to Code