JSON to Java class: from JSON to code
Paste JSON and get Java types for Jackson: a record per object by default (Java 16 and later), or a classic class with getters if you pick that flavour. Properties use camelCase, the @JsonProperty annotation keeps the original key when it differs, numbers are Integer or Double and lists are List<T>. Boxed types are used so that a null in the JSON has somewhere to go.
This page opens the converter on Java. Paste your JSON, choose the root name and the flavour, and copy the result; everything is generated in your browser and the JSON is never sent anywhere.
A real conversion
The tool opens with this JSON, an order with a customer and two items:
{
"id": 48213,
"status": "paid",
"paid_at": "2026-11-21T14:03:00Z",
"total": 389.3,
"coupon": null,
"customer": { "name": "Ana Souza", "email": "ana@example.com", "vip": true },
"items": [
{ "sku": "KB-01", "qty": 2, "price": 149.9, "tags": ["usb", "keyboard"] },
{ "sku": "MS-07", "qty": 1, "price": 89.5 }
]
}and this is what it writes for Java:
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.List;
record Root(Integer id, String status, @JsonProperty("paid_at") String paidAt, Double total, Object coupon, Customer customer, List<Item> items) {}
record Customer(String name, String email, Boolean vip) {}
record Item(String sku, Integer qty, Double price, List<String> tags) {}How each JSON value is typed
| In the JSON | In Java |
|---|---|
| string | String |
| integer | Integer |
| decimal | Double |
| true / false | Boolean |
| null | Object |
| array of items | List<Item> |
| nested object | record |
The second item of the sample has no "tags" key, and the first has one: that is why the tool treats it as optional.
Awkward keys, null and empty lists
{"user_id": 7, "display name": "Ana", "tags": [], "address": null, "scores": [9.5, 8], "meta": {"2fa": true}, "mixed": [1, "a"]}import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.List;
record Account(@JsonProperty("user_id") Integer userId, @JsonProperty("display name") String displayName, List<Object> tags, Object address, List<Double> scores, Meta meta, List<Object> mixed) {}
record Meta(@JsonProperty("2fa") Boolean field2fa) {}"user_id" becomes userId with @JsonProperty("user_id"), "display name" becomes displayName, "2fa" becomes field2fa (a Java name cannot start with a digit), and the empty array and null become List<Object> and Object.
The other flavour
The same small JSON, {"id": 1, "paid_at": "2026-11-21", "coupon": null}, in the default flavour and in the "class" one:
import com.fasterxml.jackson.annotation.JsonProperty;
record Order(Integer id, @JsonProperty("paid_at") String paidAt, Object coupon) {}import com.fasterxml.jackson.annotation.JsonProperty;
class Order {
private Integer id;
@JsonProperty("paid_at") private String paidAt;
private Object coupon;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getPaidAt() {
return paidAt;
}
public void setPaidAt(String paidAt) {
this.paidAt = paidAt;
}
public Object getCoupon() {
return coupon;
}
public void setCoupon(Object coupon) {
this.coupon = coupon;
}
}The class flavour writes private fields with getters and setters, for frameworks and libraries that do not read records yet.
Reading the JSON with Jackson
ObjectMapper mapper = new ObjectMapper();
Root order = mapper.readValue(json, Root.class);
System.out.println(order.customer().name());Records need Jackson 2.12 or newer. By default Jackson fails on unknown keys; set FAIL_ON_UNKNOWN_PROPERTIES to false (or add @JsonIgnoreProperties(ignoreUnknown = true)) when the API may grow new fields.
Common mistakes
- Using int instead of Integer for a field that can be null: Jackson then fails to deserialize a null into a primitive. Keep the boxed type the tool writes.
- Dates and money: "paid_at" is a String and "total" a Double. For a date use Instant or LocalDateTime with the jackson-datatype-jsr310 module, and for money BigDecimal.
How to use the converter
- Paste your JSON on the left (or keep the example); a mistake is reported with its line and column.
- Set the root name and the flavour; the code updates as you type.
- Copy the code or download it as a file.
Frequently asked questions
How do I convert JSON to a Java class?
Paste the JSON, choose record or class and the root name, and copy the code. Add the Jackson dependency and call readValue.
Record or class: which should I pick?
Record for Java 16+ and immutable data: less code, equals and toString included. Class when a framework needs a default constructor and setters, such as some JPA and mapper setups.
Does it work with Gson or Moshi?
The annotations are Jackson's (@JsonProperty). For Gson or Moshi replace them with @SerializedName or @Json; the shape of the classes stays the same.