Integrating an API returns JSON; you need the matching Java entity class (POJO). Hand-writing dozens of fields + getters/setters + annotations is tedious and error-prone. Can you auto-convert?
Yes. Here are the type-mapping rules and annotation choices for JSON → Java, and how to generate step by step.
JSON type to Java type mapping
| JSON type | Java type | Notes |
|---|---|---|
| Object {} | nested class / Map<String,Object> | |
| Array [] | List<T> | Prefer List over array |
| String "a" | String | |
| Integer | Long / Integer | Large numbers → Long (avoid overflow) |
| Decimal | Double / BigDecimal | Money → BigDecimal |
| Boolean | Boolean | |
| null | wrapper type (e.g. Integer) | Distinguish null from 0 |
| Mixed array | List<Object> | Avoid if possible |
Which serialization annotation
Three common Java JSON libraries, different annotations:
| Library | Annotation | Example |
|---|---|---|
| Jackson | @JsonProperty("name") | Mainstream, Spring default |
| Gson | @SerializedName("name") | Common in Android |
| Native | field name must match JSON | Not recommended |
Spring Boot defaults to Jackson—examples below use it.
Step-by-step generation (no plugin)
Use this site's JSON to Java tool—runs locally, JSON never uploaded:
- Open JSON to Java.
- Paste JSON (object or array).
- Configure:
- Root class name (e.g.
User,OrderResponse); - Package (e.g.
com.example.dto); - Generate Lombok
@Data(skip getters/setters); - Annotation style (Jackson / Gson).
- Root class name (e.g.
- Click convert—get complete Java classes, paste into project.
- Need other languages? Use JSON to Go or JSON to TypeScript.
Full example
Input JSON:
{
"id": 1024,
"name": "Alice",
"vip": true,
"tags": ["java", "api"],
"address": {
"city": "Shanghai",
"zip": "200000"
}
}
Generated Java (Jackson + Lombok):
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import java.util.List;
@Data
public class User {
@JsonProperty("id")
private Long id;
@JsonProperty("name")
private String name;
@JsonProperty("vip")
private Boolean vip;
@JsonProperty("tags")
private List<String> tags;
@JsonProperty("address")
private Address address;
}
@Data
public class Address {
@JsonProperty("city")
private String city;
@JsonProperty("zip")
private String zip;
}
Advanced tips
- Money →
BigDecimal:doublehas precision issues; always useBigDecimalfor currency. - Large integers →
Long: JSON numbers beyondintrange (2.1B) overflow. - Format first: tidy messy JSON with the JSON formatter for better inference.
- null vs zero: use wrapper classes (
Integernotint) so you can tell if a field was returned. - Time fields: JSON times are usually strings; use
StringorLocalDateTime+ custom deserializer.
FAQ
Q: Array elements inconsistent types?
Use List<Object> and judge yourself; or push the API to unify structure.
Q: Field is snake_case user_name, Java wants camelCase?
Map with @JsonProperty("user_name"); write the Java field as userName.
Q: Use Lombok? If the team standardizes on it, it cuts boilerplate massively; if banned, the tool can emit plain getters/setters.
Q: List or array?
Prefer List<T>—more flexible for dynamic length; use arrays only for specific perf/interop needs.
Summary
JSON to Java POJO = generate annotated classes by type mapping; money → BigDecimal, large numbers → Long, null → wrapper. Use this site's JSON to Java tool to generate locally—JSON not uploaded, safer for sensitive payloads.
Related Tools
Related Articles
How to Convert JSON to Go struct (with json tags and nested types)
Have a JSON API response and want the matching Go struct? Learn the JSON-to-Go type mapping rules and step-by-step how to generate code with json tags locally.
Complete Guide to HTML to JSX Conversion for React Developers
Understand the key differences between HTML and JSX, master className, style objects, camelCase attributes, and quickly migrate HTML snippets into React projects.
Regex Not Matching? Troubleshoot These 6 Common Pitfalls
How to troubleshoot when a regex doesn't match or matching fails? This article walks through 6 common pitfalls — greedy quantifiers, missing flags, unescaped special chars, lookaround assertions, newline handling, and Unicode properties — with before/after examples to help you quickly locate regex debugging issues.