Developer Tools 6 min read

How to Convert JSON to Java Classes (POJOs) in Seconds

Stop writing DTOs by hand. Learn how JSON types map to Java, how nested objects and arrays become classes, and how to handle keys that are not valid Java names.

Imaginex AI Team
November 27, 2026

The Problem

Every REST client in Java needs classes that mirror the JSON it receives. Writing them by hand is slow and error prone, especially for deeply nested responses.

How JSON Types Map to Java

JSON valueJava type
"text"String
42int (or long if larger than 2,147,483,647)
9.99double
trueboolean
{ ... }A new class
[ ... ]List
nullObject

Step by Step

  • Copy a real response from your API, ideally one with every field filled in.
  • Paste it into the [JSON to Java POJO Generator](/tools/json-to-java) and pick a root class name such as Response or User.
  • Copy the generated classes into your project. Each nested object becomes its own class with private fields, getters and setters.
  • Keys That Are Not Valid Java Names

    APIs often use keys like first-name or class. The generator converts them to camelCase fields (firstName, class_) and adds a Jackson annotation so mapping still works:

    @JsonProperty("first-name")
    

    private String firstName;

    Tips for Production Code

    • Validate the sample first with the [JSON Validator](/tools/json-validator); a trailing comma will break generation.
    • Use a sample where arrays are not empty, so the element type can be detected.
    • Explore large responses first with the [JSON Viewer](/tools/json-viewer) to pick the part you actually need.
    • With Java 16 or later, you can turn simple POJOs into records once you know the shape is stable.

    Summary

    Generating POJOs from a real JSON sample removes boilerplate and typos. Review the types, add validation annotations where needed, and you are ready to deserialize with Jackson or Gson.

    Tags
    JavaJSONPOJOJacksonSpring Boot

    Try our free tools

    Put these tips into practice with 45+ free online tools - no signup, no login.

    Browse Free Tools