JSON to Java Class
The JSON to Java Class Generator converts a JSON sample into Java POJO class definitions with private fields, getters, setters, and an optional Lombok variant. Use it to rapidly generate the model classes needed for Jackson or Gson JSON deserialisation in Spring Boot REST controllers, Android Retrofit clients, and Java microservices.
What Is the JSON to Java Class Generator?
The JSON to Java Class Generator analyses a JSON sample and produces Java POJO (Plain Old Java Object) class definitions with private fields, public getters and setters, and a no-argument constructor. Each JSON object becomes a Java class, each key becomes a private field with the appropriate Java type, and the generated code is ready for use with Jackson (ObjectMapper) or Gson — the two most widely used Java JSON libraries.
Java's strong type system requires explicit class definitions before you can work with structured data in a type-safe way. Without a POJO class, Java developers must use Map<String, Object> with type casts for every value access — a verbose and error-prone pattern. With a generated POJO, Jackson's ObjectMapper.readValue() can deserialise a JSON string directly into a typed Java object, and Gson's fromJson() does the same, giving you IDE autocomplete, compile-time type checking, and readable property access via getters.
According to Jackson's documentation, the most widely used Java JSON library, the standard approach for JSON binding is to define POJOs with JavaBean-style getters and setters, then use ObjectMapper to map between JSON and Java objects. This pattern is the foundation of Spring Boot REST controller request/response handling, Hibernate ORM configuration, and virtually every enterprise Java application that processes JSON.
This generator is used by Java developers integrating REST APIs in Spring Boot applications, Android developers parsing API responses, back-end engineers defining data transfer objects (DTOs), and developers adopting Java for a new service who need to quickly produce typed models from existing API contracts.
How to Use the JSON to Java Class Generator
- Paste your JSON sample. The input should be a representative JSON object or array. Include null values and all possible fields to get the most accurate class definition. Fields with null values are typed as
Objector the nullable wrapper type (Integerinstead ofint). - Enter a class name. Enter a PascalCase class name — for example
User,Product, orApiResponse. Nested objects generate inner classes or separate top-level classes named from the property name in PascalCase. - Choose your output style. Select Standard POJO for classes with private fields plus explicit getters and setters. Select Lombok to add
@Data,@NoArgsConstructor, and@AllArgsConstructorannotations instead of explicit accessors — Lombok generates the boilerplate at compile time. - Review type mappings. JSON strings become
String. JSON integers becomeIntegerorLong. JSON floats becomeDouble. JSON booleans becomeBoolean. JSON arrays becomeList<Type>. JSON null values produceObjector nullable wrapper types. - Copy and integrate. Paste the generated classes into your Java project. Add Jackson (
com.fasterxml.jackson.core:jackson-databind) or Gson (com.google.code.gson:gson) as a dependency, then deserialise:User user = objectMapper.readValue(jsonString, User.class);
Why Use This Tool
A JSON object with 15 fields, 3 nested objects, and 2 list properties requires approximately 80–100 lines of Java POJO code when written with explicit getters and setters — 15 private field declarations, 30 getter and setter methods, plus constructor and nested class definitions. Writing this manually is both tedious and a source of typos. This tool generates the complete class in seconds.
Spring Boot REST developers
Spring Boot uses Jackson for automatic JSON serialisation and deserialisation in REST controllers. Request body types and response types are Java classes annotated with @RequestBody and returned as response bodies. A Spring Boot developer integrating a new endpoint — or consuming an external API with RestTemplate or WebClient — needs a POJO matching the JSON shape. This generator provides that POJO from a real API response or request body sample.
Android developers parsing API responses
Android applications use Retrofit (which uses Gson or Moshi under the hood) for API integration. Retrofit requires POJO model classes matching the API response JSON before it can deserialise responses automatically. Android developers use this generator to produce the model classes from a real API response captured in Postman or a network inspector, eliminating manual transcription of field names and types.
Data transfer object (DTO) design
Enterprise Java applications use DTO classes to move data between layers — from the API layer to the service layer to the persistence layer. When a new API contract is defined (either consumed or produced), the corresponding DTOs must be written. This generator produces the initial DTO class from the JSON contract, giving developers a starting point that they can refine with validation annotations (@NotNull, @Size, @Email) and Jackson annotations (@JsonProperty, @JsonIgnore).
Microservices developers integrating third-party APIs
Java microservices frequently consume third-party APIs — payment gateways, shipping carriers, government data services. Each API response has a specific JSON shape that requires a matching Java class for type-safe handling. This generator produces those classes from API documentation samples or real responses, reducing the integration time from writing boilerplate to focusing on business logic.
Real-World Use Cases
Spring Boot developer integrating a payment gateway API
A back-end developer at a fintech company integrates a payment gateway's REST API into a Spring Boot service. The gateway returns a complex nested JSON response including transaction details, card information, merchant data, and status codes. The developer captures a real test response from the gateway's sandbox environment, pastes it into this generator, and produces the Java POJO hierarchy in seconds. The generated classes — PaymentResponse, Transaction, CardDetails, Merchant — are placed in the dto/ package and used with objectMapper.readValue() throughout the service.
Android developer building a weather app
An Android developer uses Retrofit with Gson to fetch weather data from the OpenWeatherMap API. The API response includes nested objects for main metrics, wind data, cloud coverage, and system information. Rather than writing the model classes from the API documentation, the developer pastes a real API response into this generator and produces all the model classes needed for Retrofit in under a minute. The classes are placed in the model/ package and wired into the Retrofit interface with Call<WeatherResponse>.
Data engineer building a Java ETL pipeline
A data engineer implements a Java ETL job that reads JSON records from an Apache Kafka topic — each record representing a customer order — and writes transformed data to a PostgreSQL database via JPA. The Kafka message format is defined by the upstream service team as a JSON sample. The engineer generates the Java class from the sample, adds Jackson annotations for any field name mappings, and uses it as the deserialisation target in the Kafka consumer. The downstream JPA entity is a separate class — the generated POJO serves as the intermediate transfer object.
Back-end developer creating a public API
A Java developer designs a new REST API for a marketplace platform. The API will return product listings, seller profiles, and order data. The developer prototypes the API response formats as JSON samples, then uses this generator to produce the Java POJO classes that will serve as the Spring Boot response types. Starting from the generator output, the developer adds @JsonProperty annotations for consistent camelCase output, @NotNull for validation, and Javadoc comments for API documentation generation. The generated code provides the structural scaffold; the developer adds the semantic layer.
Common Mistakes and Troubleshooting
Using primitive types instead of wrapper types for nullable fields
Java primitive types (int, double, boolean) cannot be null. If a JSON field is sometimes null, the corresponding Java field must use the wrapper type (Integer, Double, Boolean) to represent null correctly. When Jackson deserialises a JSON null into a primitive field, it throws an error. The generator uses wrapper types for any field that has a null value in the sample. Review generated classes and ensure fields that are sometimes null use wrapper types.
Missing no-argument constructor causing Jackson errors
Jackson's ObjectMapper requires a public no-argument (default) constructor to instantiate the class before setting fields. If you add a parameterised constructor to the class without also keeping the no-argument constructor, Jackson will throw a MismatchedInputException. The generator includes a no-argument constructor in the output; if you add constructors, always preserve it or annotate the parameterised constructor with @JsonCreator.
camelCase vs snake_case field name mismatches
JSON APIs often use snake_case keys (created_at, user_id) while Java convention uses camelCase (createdAt, userId). Jackson handles this mismatch via the @JsonProperty("created_at") annotation on the field or setter, or by configuring the ObjectMapper with PropertyNamingStrategies.SNAKE_CASE. The generator adds @JsonProperty annotations for any field whose JSON name does not match its Java camelCase name.
Nested class placement
The generator produces nested class definitions either as static inner classes within the parent class or as separate top-level classes. For large class hierarchies, separate top-level classes in their own files are easier to maintain. For small, tightly coupled structures, inner classes are acceptable. In Spring Boot, separate classes in the dto/ package are the standard convention. Move the generated classes into the appropriate file structure for your project.
List type erasure at runtime
Java's type system erases generic type information at runtime. When Jackson deserialises a JSON array into a List<User> field within a POJO, it uses the generic type information from the class field declaration to infer the element type. This works correctly for typed field declarations. However, if you try to deserialise a root-level JSON array using objectMapper.readValue(json, List.class), Jackson cannot infer the element type and produces a List<Map>. Use a TypeReference instead: objectMapper.readValue(json, new TypeReference<List<User>>() {}).
Frequently Asked Questions
What Java types are generated for each JSON value type?
What is a POJO and why is it used for JSON in Java?
How do I deserialise JSON into the generated Java class?
Should I use Jackson or Gson for Java JSON parsing?
What is the @JsonProperty annotation and when do I need it?
What is Lombok and how does it simplify POJO code?
How do I handle a root-level JSON array in Java?
How are nested JSON objects handled in the generated Java code?
How do I add validation annotations to the generated fields?
Can I generate Java records instead of classes?
Rate This Tool
Was this tool helpful?
Be the first to rate this tool
About the Author
S. Siddiqui is the founder and editor-in-chief of YourToolsBase, overseeing all content, tool accuracy, and editorial standards.
View full profileAuthoritative Sources
Formulas and data in this tool are based on guidelines from the above sources.