JSON to Java POJO Generator
Paste JSON, get Java classes — nested objects, List types, getters/setters, Lombok support — copy or download as .java
Options
361 characters
3 classes generated · 2,236 characters
You’re integrating a third-party REST API in Java. The response is a deeply nested JSON object with 25 fields, several arrays of sub-objects, and the occasional null. Writing the POJO by hand means 25 private fields, 50 getter/setter methods, and one wrong field name means Jackson silently ignores the value at runtime. This generator produces all of that in under a second.
What Is a POJO?
POJO stands for Plain Old Java Object — a Java class that contains private fields with public getter and setter methods, no special framework dependencies, and no required superclass. POJOs are the standard model class for JSON deserialization in Java using libraries like Jackson (com.fasterxml.jackson) and Gson (com.google.gson).
A typical POJO for a User JSON object looks like this:
public class User {
private String name;
private int age;
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public int getAge() { return age; }
public void setAge(int age) { this.age = age; }
}
When you have JSON with 10, 20, or 50 fields and several nested objects, writing this by hand becomes a significant and error-prone task. This tool automates it entirely.
Why Generate from JSON?
API Integration: Paste the raw response from any REST API to get Java classes that match the JSON structure exactly. Then use Jackson to deserialize the response directly into your POJO:
ObjectMapper mapper = new ObjectMapper();
Root root = mapper.readValue(jsonString, Root.class);
Configuration Parsing: If your application reads JSON configuration files, generating the model classes from a sample config gives you type-safe access to every field.
Avoiding Silent Failures: Jackson ignores fields that don’t match the POJO by default. If your POJO field is named user_name but the JSON key is userName, you get null at runtime with no error. Getting the names right from the start prevents this.
Lombok Support
Lombok is a Java annotation processor that eliminates boilerplate code. With the @Data annotation enabled in this tool, each class is generated with @Data at the top:
@Data
public class User {
private String name;
private int age;
// No getters or setters needed — Lombok generates them at compile time
}
The @Data annotation generates: getters for all fields, setters for all non-final fields, toString(), equals(), hashCode(), and a required-args constructor.
To use Lombok, add it as a dependency:
Maven:
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
<scope>provided</scope>
</dependency>
Gradle:
compileOnly 'org.projectlombok:lombok:1.18.30'
annotationProcessor 'org.projectlombok:lombok:1.18.30'
Nested Objects
When a JSON value is itself an object, the generator creates a separate class named after the parent class and the field key in PascalCase:
{ "address": { "street": "123 Main St", "city": "Springfield" } }
Produces Root (with an Address address field) and a separate RootAddress class. Each nested class appears in the output, ready to copy into its own .java file.
Arrays and List Types
JSON arrays map to List<T> types using java.util.List. The import statement is added automatically when needed. When an array contains objects, the generator merges all objects in the array into a single class:
{ "posts": [{ "id": 1, "title": "Hello" }, { "id": 2, "title": "World", "published": true }] }
Generates a RootPostsItem class with id, title, and published fields — all fields from all elements are included.
Primitive vs Wrapper Types
- Primitives (default):
int,double,boolean— more memory-efficient, cannot benull - Wrapper types:
Integer,Double,Boolean— can benull, required for use in generics and when a field might be absent
Choose wrapper types if your JSON fields can be null or may be absent in some responses.
What to Add After Generating
The generated POJOs are a solid starting point. Consider these refinements:
-
Jackson annotations for non-standard JSON keys:
@JsonProperty("created_at") private LocalDateTime createdAt; -
Validation annotations using Bean Validation:
@NotBlank private String email; -
Deduplicate identical classes — if two generated classes are structurally identical, consolidate them.
FAQ
Does this tool send my JSON to a server?
No. All processing runs entirely in your browser using JavaScript. Your JSON data never leaves your device.
What imports are needed?
If the generated code uses List, the tool outputs import java.util.List; at the top. If Lombok is enabled, import lombok.Data; is added. For Jackson deserialization, you will additionally need import com.fasterxml.jackson.databind.ObjectMapper; in your calling code.
What if my JSON root is an array?
The tool generates an item class (e.g., RootItem) and adds a comment showing how to deserialize the array using TypeReference. Use List<RootItem> as your deserialization target.
How do I handle null JSON values?
JSON null maps to Object (since the actual type is unknown). Refine these fields manually to the appropriate nullable type — typically a wrapper type (Integer, String) or a domain-specific nullable type.
Can I use this with Gson instead of Jackson?
Yes. The generated POJOs are plain Java classes with no Jackson-specific annotations by default. Gson’s fromJson method works with any POJO. For field name mapping with Gson, add @SerializedName("key") annotations manually.