Convert a Flat JSON Object to a Basic Java POJO
Generate a plain old Java object (POJO) with private fields, getters, and setters from a flat JSON object. Covers String, Integer, Long, Double, and Boolean type mapping.
Detailed Explanation
From a Flat JSON Object to a Java POJO
The simplest JSON-to-Java conversion takes a flat object — no nesting, no arrays — and produces a class with one private field per key, plus a matching pair of accessor methods.
Example JSON
{
"id": 42,
"name": "Alice",
"email": "alice@example.com",
"active": true,
"score": 98.5
}
Generated Java POJO
package com.example.model;
public class User {
private Integer id;
private String name;
private String email;
private Boolean active;
private Double score;
public Integer getId() { return id; }
public void setId(Integer id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
// ... and so on for the remaining fields
}
Type Mapping
| JSON value | Java type |
|---|---|
| string | String |
| integer | Integer (or Long for values beyond 32-bit range) |
| decimal | Double |
| boolean | Boolean |
| null | Object (cannot infer further) |
| array | List<T> with recursively inferred T |
Why Boxed Types?
The generator emits the boxed wrapper types (Integer, Long, Double, Boolean) rather than the primitives (int, long, double, boolean) because JSON allows null for any field. Boxed types can hold null; primitives cannot. If you know a field is always present, you are free to swap to a primitive after generation.
Usage with Jackson
ObjectMapper mapper = new ObjectMapper();
User user = mapper.readValue(json, User.class);
This pattern is the foundation every other example builds on. Once you have the basic POJO, adding Lombok, switching to a record, or annotating for Jackson are all incremental refinements.
Use Case
Reading a simple REST API response — a single resource lookup by ID, a configuration row from a key-value store, or a webhook payload with a flat structure — is the most common starting point for any Java service consuming JSON.