JSON to Java class
Free JSON to Java class generator: create POJOs with getters and setters from sample JSON, or switch to C# classes and Go structs with tags. Runs in your browser.
How to use JSON to Java Class
- 1Paste a representative JSON sample, or press Try example.
- 2Pick Java, C# or Go, set the root class name and the language options.
- 3Copy the generated code, or download it as a source file.
What makes it better
- Java POJOs with private fields, getters and setters, optional @JsonProperty
- C# classes with PascalCase properties and optional [JsonPropertyName]
- Go structs with json tags, omitempty and pointers for optional fields
- Nested classes extracted automatically, lists for arrays
- Whole numbers become Integer or Long, decimals Double
About JSON to Java Class
JSON to Java Class turns a sample document into model classes. Java output uses private fields with optional getters and setters and List<T> for arrays; the language switch also produces C# properties in PascalCase and Go structs with json tags. Nested objects become their own classes, and array items are merged so that fields missing from some items are handled.
Common uses
- Create the model classes to deserialize an API response with Jackson, Gson or System.Text.Json.
- Draft Go structs for encoding/json from a sample payload.
- Start a data transfer object from real data instead of typing every field.
- Translate one sample into several languages for a multi-language client library.
Example
Input
{"id":101,"name":"Ada","tags":["math","code"],"address":{"city":"London"}}Result
import java.util.List;
public class Root {
private Integer id;
private String name;
private List<String> tags;
private Address address;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<String> getTags() {
return tags;
}
public void setTags(List<String> tags) {
this.tags = tags;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
class Address {
private String city;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
}Good to know
- Classes are inferred from the sample only, so use one that shows every field. Values that are always null, or arrays that are empty, become Object, object or any.
- Java fields use boxed types such as Integer and Double so that missing values can be null. Mixed values become Object.
- Generated code is a starting point. Add the imports, packages or namespaces your project needs, and check naming against your conventions.
JSON to Java Class FAQ
How do I convert JSON to a Java class?
Paste a sample into the box with Java selected. You get a class named Root with one private field per key, getters and setters, and separate classes for each nested object. Rename Root with the root name field.
Can I get public fields instead of getters and setters?
Yes. Untick Getters and setters to produce public fields. Keep it ticked for the JavaBean style that most serialization libraries expect.
What happens when a JSON key is not a valid Java name?
The field name is converted to camelCase, a keyword such as class gets an underscore, and names that start with a digit get a prefix. Tick @JsonProperty to keep the original key mapped for Jackson.
How does the C# output differ?
C# classes use PascalCase auto-properties such as public string UserName { get; set; }, List<T> for arrays and nullable types like int? for optional fields. [JsonPropertyName] attributes preserve the original key names.
How are Go structs generated?
Each object becomes a struct with exported PascalCase fields and a json tag holding the original key. Optional or null fields become pointers with omitempty, arrays become slices and common initialisms such as ID and URL are capitalised.