How to use the json to java class converter
- Paste your JSON object or array into the JSON input field.
- Set the Root class name for the top-level Java class.
- The generated Java code appears automatically as you type.
- Copy the output into your IDE or project source folder.
How the conversion works
The converter parses the JSON input and walks every key-value pair to build a Java class. Strings map to String, whole numbers to int or long, decimals to double, booleans to boolean and null values to Object. When it encounters a nested object, it creates a new static inner class. Arrays become List<T> where T is the inferred element type.
For example, given {"name":"Ada","scores":[98,100]}, the tool produces a String name field and a List<Integer> scores field inside the root class. Keys are converted from snake_case or kebab-case to camelCase for idiomatic Java naming.
Type mapping reference
This table shows how each JSON type translates to a Java type during the json to java class conversion.
| JSON type | Example | Java type |
|---|---|---|
| string | "hello" | String |
| integer (fits 32-bit) | 42 | int |
| integer (large) | 3000000000 | long |
| decimal | 3.14 | double |
| boolean | true | boolean |
| null | null | Object |
| array | [1,2] | List<Integer> |
| object | {"a":1} | Inner class |
Naming conventions applied
JSON keys often use snake_case (first_name) or kebab-case (first-name). The converter transforms these into camelCase (firstName) for Java field names. The class name itself is PascalCased. If you provide a root class name like user_profile, it becomes UserProfile.
When two different objects share the same derived class name, the tool appends a numeric suffix to avoid collisions. For instance, if both an address field and a nested address object exist at different levels, they may become Address and Address2.
Handling arrays and mixed types
Arrays are mapped to java.util.List. The element type is inferred from all non-null items: whole numbers mixed with decimals become Double, and other mixed types such as strings and numbers fall back to Object. Empty arrays also default to List<Object> because there is no data to infer from.
When an array holds objects, the converter generates a single inner class by merging all unique keys found across every object in that array. This ensures the resulting Java class can hold any element from the list without missing fields.
Worked examples
| Example | Input | Result |
|---|---|---|
| Simple user object | JSON input: {"name":"Ada","age":36,"active":true}; Root class name: User | public class User { / private String name; / private int age; / private boolean active; / public User() { / } / public User(String name, int age, boolean active) { / this.name = name; / this.age = age; / this.active = 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 getActive() { / return active; / } / public void setActive(boolean active) { / this.active = active; / } / } |
| Nested address | JSON input: {"person":{"firstName":"John","lastName":"Doe"},"city":"London"}; Root class name: Record | public class Record { / private Person person; / private String city; / public Record() { / } / public Record(Person person, String city) { / this.person = person; / this.city = city; / } / public Person getPerson() { / return person; / } / public void setPerson(Person person) { / this.person = person; / } / public String getCity() { / return city; / } / public void setCity(String city) { / this.city = city; / } / public static class Person { / private String firstName; / private String lastName; / public Person() { / } / public Person(String firstName, String lastName) { / this.firstName = firstName; / this.lastName = lastName; / } / 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; / } / } / } |
| Array of items | JSON input: [{"id":1,"tag":"alpha"},{"id":2,"tag":"beta"}]; Root class name: ItemList | public class ItemList { / private int id; / private String tag; / public ItemList() { / } / public ItemList(int id, String tag) { / this.id = id; / this.tag = tag; / } / public int getId() { / return id; / } / public void setId(int id) { / this.id = id; / } / public String getTag() { / return tag; / } / public void setTag(String tag) { / this.tag = tag; / } / } |
FAQ
Does this tool upload my JSON to a server?
No. The json to java class conversion happens entirely in your browser using JavaScript. Your data never leaves your device.
What happens when a JSON value is null?
Null values map to the Object type since there is no runtime information to determine a more specific type. If another object in the same array provides a typed value for that key, the non-null type is used instead.
Can I convert a JSON array directly?
Yes. Paste a JSON array and the tool merges all objects in the array to produce a single root class containing every unique key found across the elements.
Are the generated classes ready for Jackson or Gson?
The classes include fields, a no-arg constructor, an all-args constructor, getters and setters. When a JSON key has to be renamed to a valid Java name (for example first_name becomes firstName, or class becomes class_), the tool adds a Jackson @JsonProperty or Gson @SerializedName annotation with the original key so deserialization still matches. Choose None if you prefer to add your own mapping.
How are nested objects handled?
Each nested JSON object becomes a public static inner class inside its parent class. This keeps everything in a single file while preserving the hierarchy.
What if my JSON keys contain spaces or special characters?
Keys are sanitized to form valid Java identifiers. Spaces, hyphens and other symbols trigger camelCase conversion, names that start with a digit get an f prefix, Java keywords such as class or new get a trailing underscore, and two keys that map to the same name get a numeric suffix. Characters outside A to Z, a to z and 0 to 9 are not kept.