JSON to Java Converter
Generate Java POJO classes from JSON data. Paste your JSON and configure options:
Generate Java Classes from JSON
This tool automatically generates Java class definitions from your JSON data. It infers types from values, handles nested objects, and supports popular libraries like Lombok, Jackson, and Gson.
Output Options
Standard POJO with Lombok
Using Lombok's @Data annotation eliminates boilerplate by auto-generating getters, setters, toString, equals, and hashCode:
@Data
public class User {
@JsonProperty("user_name")
private String userName;
private Integer age;
private List<String> roles;
}Java 16+ Records
Records are immutable data classes with concise syntax. Ideal for DTOs:
public record User(
@JsonProperty("user_name") String userName,
Integer age,
List<String> roles
) {}Jackson vs Gson
| Feature | Jackson | Gson |
|---|---|---|
| Annotation | @JsonProperty | @SerializedName |
| Speed | Faster for large payloads | Simpler for small data |
| Spring Boot | Default choice | Requires configuration |
Type Inference
The converter maps JSON types to Java:
| JSON | Java | Notes |
|---|---|---|
| string | String | |
| integer | Integer / Long | Long for values > 2.1B |
| decimal | Double | |
| boolean | Boolean | |
| array | List<T> | Type from first element |
| object | Nested class | Separate class generated |
| null | Object |
Example Conversion
Input JSON:
{
"id": 1,
"username": "john_doe",
"profile": {
"firstName": "John",
"age": 30
}
}Generated Java (with Lombok + Jackson):
package com.example.model;
import java.util.List;
import lombok.Data;
import com.fasterxml.jackson.annotation.JsonProperty;
@Data
public class Root {
private Integer id;
private String username;
private Profile profile;
}
@Data
public class Profile {
@JsonProperty("firstName")
private String firstName;
private Integer age;
}Using Generated Classes
With Jackson
ObjectMapper mapper = new ObjectMapper();
// Deserialize
String json = getJsonFromApi();
Root data = mapper.readValue(json, Root.class);
// Serialize
String output = mapper.writeValueAsString(data);With Gson
Gson gson = new Gson();
// Deserialize
Root data = gson.fromJson(json, Root.class);
// Serialize
String output = gson.toJson(data);Best Practices
- Use Lombok — Eliminates boilerplate getters/setters
- Use wrapper types —
Integerinstead ofintto handle nulls - Add validation — Consider adding
@NotNull,@Sizeannotations manually - Use records for DTOs — Immutable, thread-safe, less code
Related Tools
- JSON Validator — Validate JSON before converting
- JSON to C# — Generate C# classes
- JSON to TypeScript — Generate TS interfaces
- JSON to Python — Generate Python dataclasses
- JSON Schema Validator — Validate structure
Frequently Asked Questions
Should I use Lombok or write getters/setters?
Lombok is recommended for most projects — it reduces boilerplate significantly. However, some teams prefer explicit code for debugging or IDE support reasons.
When should I use Records vs Classes?
Use records for immutable data transfer objects (DTOs). Use classes when you need mutability, inheritance, or complex behavior.
How do I handle null values?
The converter uses wrapper types (Integer, Boolean) which can be null. For primitives that shouldn't be null, change toint, boolean manually.