Turn a JSON sample into Java records or Lombok @Data classes: camelCase names, @JsonProperty for other keys, long for big ids
Paste an API response and get Java records, or Lombok classes, ready for Jackson to read.
How to use it
Paste a JSON sample, or open a .json file. Choose records (Java 16+) or Lombok @Data classes, and set the root class’s name. Copy the code or download it as a .java file named after the root class.
Examples
Records with a renamed key
Input
{"order_id": 9007199254740993, "paid": true, "items": [{"sku": "MUG-01", "qty": 2}]}Result
import java.util.List;
import com.fasterxml.jackson.annotation.JsonProperty;
public record Root(
@JsonProperty("order_id") long orderId,
boolean paid,
List<Item> items
) {}
record Item(
String sku,
int qty
) {}order_id becomes orderId with @JsonProperty naming the key; the id needs 64 bits, so it is long.
A Lombok class
Input
{"name": "Ada", "nickname": null}Result
import lombok.Data;
@Data
public class Root {
private String name;
private Object nickname;
}How the types are read
Every object gets its own named type; the name comes from its key (customer → Customer, line_items → LineItem). The objects in one array are merged into one type, so a field missing from some of them is marked optional. A field that is null in one place and a string in another is a nullable string. A field that is only ever null, or mixes kinds such as 1 and "a", gets the language’s “anything” type. Integers and decimals are told apart by how they are written: 25 is an integer, 25.0 and 12.5 are not. An integer past 2,147,483,647 gets a 64-bit type, and one past 64 bits a big-number type, read from the digits, never rounded. Empty arrays get a list of “anything”, since the sample doesn’t say what they hold.
Java specifics
Names are camelCase; when that differs from the key, @JsonProperty keeps the key for Jackson (records need Jackson 2.12 or later). Fields that may be missing or null use the boxed type (Integer, Long, Double, Boolean), so they can hold null. Integers get int, long past 32 bits, and BigInteger past 64 bits. The root is public and the others are package-private, so the file compiles as one .java file. Move them to files of their own as you like. A key that is a Java keyword gets an underscore: class becomes class_.
Questions
- Does it work with Gson?
Gson reads the classes too, but it doesn’t know @JsonProperty: change those to @SerializedName("key"), and use the Lombok style, since older Gson versions can’t create records. - Should I pick records or Lombok classes?
Records need Java 16 or later and Jackson 2.12 or later, and no extra library. Pick Lombok @Data classes for an older Java, when you need setters, or for an older Gson, which can’t create records. - Is my JSON uploaded?
No. Everything runs in your browser, so your JSON stays on your device. Nothing you paste is uploaded unless you share a result, which is locked with your password first.