JSON to Java POJO Generator
Last updated:
Jsonic's JSON to Java POJO generator converts a JSON object into Java class definitions using Jackson annotations. Each field gets a @JsonProperty("key") annotation to preserve the original JSON key. JSON strings map to String, integers to long, decimals to double, booleans to boolean, and null values to the corresponding object type (String, Long). Arrays become List<T> with a java.util.List import. Nested objects generate separate named classes declared before the root class. Private fields are generated with public getters and setters following Java naming conventions. All processing runs in your browser with no data upload.
How to convert JSON to a Java POJO
- Paste your JSON object into the left panel, or click Example to load a sample.
- Optionally change the root class name (default: Root).
- Click Generate.
- Add Jackson to your project: implementation("com.fasterxml.jackson.core:jackson-databind:2.x.x") for Gradle or the equivalent Maven dependency.
- Deserialize with: Root obj = new ObjectMapper().readValue(jsonString, Root.class);
FAQ
Which JSON library does the generated code use?
The generated code uses Jackson, specifically the @JsonProperty annotation from com.fasterxml.jackson.annotation. To use it, add jackson-annotations and jackson-databind to your project. For Maven: <dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.17.0</version></dependency>. For Gradle: implementation("com.fasterxml.jackson.core:jackson-databind:2.17.0"). jackson-databind pulls in jackson-annotations automatically.
How are JSON types mapped to Java types?
JSON strings → String, JSON integers → long (primitive), JSON decimals → double (primitive), JSON booleans → boolean (primitive), JSON null → the corresponding object type (String, Long, Double, Boolean), JSON arrays → List<T> with a java.util.List import, JSON objects → a named Java class. Primitive types are used for non-null fields to avoid unnecessary boxing overhead; the object wrappers (Long, Double, Boolean) are used when the field can be null.
How do I deserialize JSON with the generated class?
Use ObjectMapper from jackson-databind: ObjectMapper mapper = new ObjectMapper(); Root obj = mapper.readValue(jsonString, Root.class);. For JSON from a file: Root obj = mapper.readValue(new File("data.json"), Root.class);. ObjectMapper is thread-safe and should be created once and reused. The @JsonProperty annotations on each field tell Jackson which JSON key maps to which Java field.
Why are primitive types used instead of wrapper types?
Primitive types (long, double, boolean) are used for fields whose JSON values are never null, avoiding the overhead of boxing and reducing the risk of NullPointerExceptions. When a field is observed to be null in the JSON input, the generator switches to the wrapper type (Long, Double, Boolean) so the field can hold a null value correctly. If you know a field will always be present, you can manually change wrapper types back to primitives.
How are nested objects handled?
Each nested JSON object becomes a separate Java class. Nested classes are declared before the class that references them in the output, so the file compiles without forward-reference issues. The parent class holds a field typed to the nested class (e.g., private Address address;). You can place all the generated classes in a single file or split them into separate .java files — both approaches work with Jackson.
Can I use this with Spring Boot?
Yes. Spring Boot includes Jackson by default, so no extra dependency is needed. The generated POJO classes work directly as @RequestBody parameters in Spring MVC controllers: @PostMapping("/users") public ResponseEntity<Void> create(@RequestBody Root model). Spring deserializes the incoming JSON into the POJO automatically. The @JsonProperty annotations ensure snake_case JSON keys are mapped to camelCase Java fields correctly.
How do I handle camelCase vs snake_case JSON keys?
The generator adds a @JsonProperty("snake_case_key") annotation on each field whose JSON key differs from the Java field name. This tells Jackson exactly which key to read from the JSON. Alternatively, you can configure ObjectMapper globally: mapper.setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE); — this removes the need for per-field annotations, but then you must remove or sync the @JsonProperty annotations manually to avoid conflicts.
Should I use Lombok instead of manual getters/setters?
Yes, Lombok is a popular choice for eliminating boilerplate. Add Lombok to your build, then replace the generated class body with @Data @NoArgsConstructor on the class. Lombok generates all getters, setters, equals, hashCode, and toString at compile time. Keep the @JsonProperty annotation on each field — Lombok respects it and Jackson will still use it during deserialization. This reduces the generated class to just field declarations plus annotations.