Skip to content
Code2026-08-282 min read

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:

  1. Open JSON to Java.
  2. Paste JSON (object or array).
  3. Configure:
    • Root class name (e.g. User, OrderResponse);
    • Package (e.g. com.example.dto);
    • Generate Lombok @Data (skip getters/setters);
    • Annotation style (Jackson / Gson).
  4. Click convert—get complete Java classes, paste into project.
  5. 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: double has precision issues; always use BigDecimal for currency.
  • Large integers → Long: JSON numbers beyond int range (2.1B) overflow.
  • Format first: tidy messy JSON with the JSON formatter for better inference.
  • null vs zero: use wrapper classes (Integer not int) so you can tell if a field was returned.
  • Time fields: JSON times are usually strings; use String or LocalDateTime + 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.


Advertisement