What JSON to Java does
JSON to Java turns a sample JSON document into Java code: one class with public fields per object, with a type for every field inferred from the value in the sample.
JSON to Java examples
The built-in sample
Input
{"name":"Ada Lovelace","active":true,"tags":["math","code"],"address":{"city":"London"}}Output
import java.util.List;
public class Address {
public String city;
}
public class Root {
public String name;
public boolean active;
public List<String> tags;
public Address address;
}This is the input that the Load sample button fills in.
Awkward keys, a null, a decimal and arrays
Input
{"order-id":7,"total":19.5,"paid":false,"coupon":null,"items":[{"sku":"A1","qty":2},{"sku":"B2","qty":1,"gift":true}],"tags":[]}Output
import java.util.List;
public class Items {
public String sku;
public int qty;
public boolean gift;
}
public class Root {
public int order_id;
public double total;
public boolean paid;
public Object coupon;
public List<Items> items;
public List<Object> tags;
}The sample has a key with a hyphen (order-id), a null (coupon), a decimal (total), an array of objects whose second item has an extra field, and an empty array.
Each output was produced by running this tool on the input above it.
How it works
The sample is walked recursively and each value is mapped to a Java type:
| Sample value | Java type |
|---|---|
| String | String |
| Whole number | int |
| Decimal number | double |
| true / false | boolean |
| null, or an empty array’s items | Object |
| Array of T | List<T> |
| Object | a new named type |
Using the generated code
Jackson and Gson both fill public fields without getters or setters. With Jackson:
Root root = new ObjectMapper().readValue(json, Root.class);Details and limits
- A Java file may contain only one public top-level class. Put each generated class in its own file, or remove
publicfrom all but one. - A key that is not a valid identifier is renamed with underscores. Map it back with
@JsonProperty("order-id")(Jackson) or@SerializedName(Gson). - A key that is a reserved word, such as
class, is emitted unchanged and will not compile until you rename it. - Whole numbers are
int. Change the field tolongfor values above about two billion. - A type is named after the key that holds the object:
itemsgives a type calledItems, with no attempt to make it singular. The top-level type is alwaysRoot. - If two different objects sit under the same key name in different places, two types with the same name are generated. Rename one of them.
- Objects in an array are merged into one type. A field that only some of them have is still generated as a required field.
- Input that is not valid JSON stops the tool, and the status line shows where the problem is. The JSON Validator page lists the usual causes.
Questions
Is my data uploaded when I use JSON to Java?
No. The tool runs in your browser, so what you paste or open is processed on your own device and is not sent to a server.
What does JSON to Java do?
JSON to Java turns a sample JSON document into Java code: one class with public fields per object, with a type for every field inferred from the value in the sample.
Are there any limits I should know about?
A Java file may contain only one public top-level class. Put each generated class in its own file, or remove public from all but one. A key that is not a valid identifier is renamed with underscores. Map it back with @JsonProperty("order-id") (Jackson) or @SerializedName (Gson).
Is there a size limit?
There is no fixed size limit. Everything happens on your device, so very large inputs depend on its memory and can take longer on a phone than on a computer.
Does it work on phones, and is it free?
Yes. It works in any current browser on Android phones, iPhones, tablets and computers. It is free and needs no account or sign-up.