JSON to Java Converter
Convert JSON to Java Online
Use our free json to java converter to generate well-structured Java classes from any JSON data instantly. Whether you are building json to class definitions for Spring Boot applications, creating POJOs for Android development, or scaffolding data models for enterprise backends, this tool produces clean, compilable Java code from your JSON input without any installation required.
What is JSON Format
JSON, short for JavaScript Object Notation, is a lightweight data interchange format that has become the universal standard for web-based data communication. Its straightforward syntax uses key-value pairs enclosed in curly braces for objects and square brackets for arrays, making it easy to read and write for both humans and machines. JSON supports strings, numbers, booleans, null, nested objects, and arrays as value types.
JSON has become the default format for REST API responses, microservice communication, NoSQL database storage, and application configuration. Every modern programming language provides robust support for parsing and generating JSON data. In the Java ecosystem specifically, libraries like Jackson, Gson, and JSON-B handle serialization and deserialization between JSON and Java objects, making JSON the primary data exchange format for Java web applications and services.
What is Java Class Format
Java classes are the fundamental building blocks of Java applications, defining the structure and behavior of objects through fields, constructors, and methods. A Plain Old Java Object, commonly known as a POJO, is a simple class that contains private fields with public getter and setter methods, following the JavaBeans convention. POJOs serve as data transfer objects, entity models, and configuration holders throughout Java applications.
Java classes enforce strong typing, requiring every field to have an explicitly declared type such as String, int, double, boolean, or a reference to another class. Collections like List and Map provide typed containers for arrays and nested objects. Modern Java development often uses annotations from libraries like Jackson or Lombok to reduce boilerplate code, with annotations like JsonProperty for custom field mapping and Data for automatic getter and setter generation. Java classes are compiled to bytecode that runs on the Java Virtual Machine, ensuring platform independence across operating systems.
How the Conversion Works
Converting JSON to Java involves analyzing the structure and values of a JSON document to generate properly typed Java class definitions. The converter parses the input JSON, examines each property value to infer its Java type, and produces class files with private fields, a default constructor, and public getter and setter methods following the JavaBeans convention. Nested JSON objects generate separate Java classes, and arrays produce fields typed with Java List collections.
The conversion engine maps JSON types to their Java equivalents: strings become String, integers become int or Integer, floating-point numbers become double or Double, booleans become boolean, and null values result in nullable wrapper types. If your data originates in another format, you can first convert YAML to JSON before generating Java classes. For working with your JSON data in other ways, try our JSON to XML converter for enterprise XML integration, or explore the JSON to TypeScript converter for frontend type definitions. You might also find our Base64 encoding tool useful for handling binary data in your Java applications.
Syntax Comparison
The mapping between JSON data and Java classes follows clear patterns. A JSON object like this:
// JSON
{
"name": "Alice",
"age": 30,
"active": true
}
Generates the following Java class:
// Java
public class Root {
private String name;
private int age;
private boolean active;
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public int getAge() { return age; }
public void setAge(int age) { this.age = age; }
public boolean isActive() { return active; }
public void setActive(boolean active) { this.active = active; }
}
JSON arrays of objects produce List-typed fields with a separate class for the element type. JSON numbers without decimal points map to int, while numbers with decimals map to double. Nested JSON objects generate additional Java classes that are referenced as field types in the parent class. The converter follows standard Java naming conventions, converting snake_case or kebab-case JSON keys to camelCase field names.
Common Use Cases
The most common use case for JSON to Java conversion is creating data transfer objects for REST API integration. Java developers consuming external APIs need POJO classes that match the JSON response structure for deserialization with Jackson or Gson. Rather than manually writing these classes by reading API documentation, developers can paste a sample response and instantly generate the required class hierarchy.
Another frequent scenario involves Android development, where JSON responses from backend services need to be deserialized into Java objects for display in the user interface. Spring Boot developers also use this conversion to quickly scaffold request and response model classes when building new API endpoints. Enterprise integration projects that receive JSON data from modern microservices but need to map it into existing Java domain models benefit from having an accurate starting point generated automatically from sample data.
JSON to Java Examples
The following examples demonstrate how various JSON structures are converted into Java class definitions. These cover the most common patterns encountered when generating POJOs from API responses and data models.
A JSON object with nested structures generates multiple related classes:
// JSON Input
{
"employee": {
"id": 101,
"firstName": "John",
"lastName": "Smith",
"department": {
"name": "Engineering",
"floor": 3
},
"skills": ["Java", "Spring", "SQL"]
}
}
// Java Output
public class Department {
private String name;
private int floor;
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public int getFloor() { return floor; }
public void setFloor(int floor) { this.floor = floor; }
}
public class Employee {
private int id;
private String firstName;
private String lastName;
private Department department;
private List<String> skills;
public int getId() { return id; }
public void setId(int id) { this.id = id; }
public String getFirstName() { return firstName; }
public void setFirstName(String firstName) { this.firstName = firstName; }
public String getLastName() { return lastName; }
public void setLastName(String lastName) { this.lastName = lastName; }
public Department getDepartment() { return department; }
public void setDepartment(Department department) { this.department = department; }
public List<String> getSkills() { return skills; }
public void setSkills(List<String> skills) { this.skills = skills; }
}
Frequently Asked Questions
Does the converter generate Jackson annotations?
The converter generates plain POJO classes by default without library-specific annotations. However, the generated classes are fully compatible with Jackson, Gson, and other JSON serialization libraries out of the box since they follow the standard JavaBeans naming convention. If your JSON keys use snake_case or other non-standard naming, you can add Jackson annotations like @JsonProperty("field_name") to the generated fields to ensure correct mapping during deserialization.
How are JSON numbers mapped to Java types?
JSON numbers without a decimal point are mapped to the Java int type by default, or long if the value exceeds the integer range. Numbers with decimal points are mapped to double. If you need more precise control over numeric types, such as using BigDecimal for financial calculations, you can modify the generated field types after conversion. The converter uses primitive types where possible for better performance, but switches to wrapper types like Integer and Double when null values are present.
Can I use the generated classes with Spring Boot?
Yes, the generated Java classes work seamlessly with Spring Boot applications. You can use them directly as request body types in controller methods annotated with @RequestBody, as response types returned from service methods, or as entity models for data persistence. Spring Boot uses Jackson for JSON serialization by default, and the generated POJOs follow the conventions that Jackson expects for automatic binding.
How are nested JSON objects handled?
Each nested JSON object generates a separate Java class with its own fields, getters, and setters. The parent class references the child class as a field type, creating a clean class hierarchy that mirrors the JSON structure. This approach follows Java best practices of keeping classes focused and single-purpose rather than creating deeply nested inner classes. Class names are derived from the JSON key names using PascalCase convention.
What happens with JSON arrays in the Java output?
JSON arrays are mapped to java.util.List fields with appropriate generic type parameters. An array of strings becomes List<String>, an array of numbers becomes List<Integer> or List<Double>, and an array of objects becomes a List of the generated child class type. The converter includes the necessary import statements for List and any other collection types used in the generated code.
Does the tool support Java records?
The converter generates traditional POJO classes with getters and setters for maximum compatibility across Java versions. Java records, introduced in Java 14, provide a more concise syntax for immutable data carriers but require Java 14 or later. If your project uses a modern Java version and you prefer records, you can refactor the generated POJO into a record declaration by extracting the fields into the record component list and removing the getter and setter methods.
How does this compare to generating TypeScript or Python types?
Java class generation produces more verbose output than TypeScript interfaces or Python dataclasses because Java requires explicit getter and setter methods, access modifiers, and type declarations. TypeScript interfaces are compile-time only constructs, while Java classes produce actual bytecode. Python dataclasses fall somewhere in between with their decorator-based approach. For other language options, try our JSON to TypeScript converter or JSON to Python converter.
FAQ
How does JSON to Java Converter work?
Generate Java classes from JSON.