JSON to Java class (POJO)
The tool writes a root class with static nested classes for inner objects, ready to read with a Jackson ObjectMapper.
Loading the tool…
Processing and privacy: The result is calculated inside your browser and your data is not sent to any server.
How to use it
- Enter json input, root type name and add getters and setters.
- The result appears straight away and updates as you edit.
- Copy the result, or copy a link that keeps the same values to share it.
How it is calculated
String, int or long depending on the number size, double, boolean, List<T> and Object, with a static nested class per inner object. A field that can be null or missing uses the boxed type such as Integer or Double, and each field has @JsonProperty with the original key.
Example
Inputs: Root type name = Root, Add getters and setters = yes. Result: Generated code: import com.fasterxml.jackson.annotation.JsonProperty; import java.util.List; public class Root { @JsonProperty("id") private int id; @JsonProperty("name") private String name; @JsonProperty("email") private String email; @JsonProperty("is_active") private boolean isActive; @JsonProperty("balance") private double balance; @JsonProperty("tags") private List<String> tags; @JsonProperty("address") private Address address; @JsonProperty("orders") private List<Order> orders; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public boolean getIsActive() { return isActive; } public void setIsActive(boolean isActive) { this.isActive = isActive; } public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; } 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; } public List<Order> getOrders() { return orders; } public void setOrders(List<Order> orders) { this.orders = orders; } public static class Address { @JsonProperty("city") private String city; @JsonProperty("postal_code") private String postalCode; public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getPostalCode() { return postalCode; } public void setPostalCode(String postalCode) { this.postalCode = postalCode; } } public static class Order { @JsonProperty("order_id") private String orderId; @JsonProperty("total") private double total; @JsonProperty("coupon") private String coupon; public String getOrderId() { return orderId; } public void setOrderId(String orderId) { this.orderId = orderId; } public double getTotal() { return total; } public void setTotal(double total) { this.total = total; } public String getCoupon() { return coupon; } public void setCoupon(String coupon) { this.coupon = coupon; } } }; Types generated: 3; Fields: 13.
{
"id": 1024,
"name": "Sara Ahmed",
"email": "sara@example.com",
"is_active": true,
"balance": 1520.75,
"tags": ["vip", "riyadh"],
"address": { "city": "Riyadh", "postal_code": "12211" },
"orders": [
{ "order_id": "A-1", "total": 99.5, "coupon": "EID10" },
{ "order_id": "A-2", "total": 120, "coupon": null }
]
}import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.List;
public class Root {
@JsonProperty("id")
private int id;
@JsonProperty("name")
private String name;
@JsonProperty("email")
private String email;
@JsonProperty("is_active")
private boolean isActive;
@JsonProperty("balance")
private double balance;
@JsonProperty("tags")
private List<String> tags;
@JsonProperty("address")
private Address address;
@JsonProperty("orders")
private List<Order> orders;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public boolean getIsActive() {
return isActive;
}
public void setIsActive(boolean isActive) {
this.isActive = isActive;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
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;
}
public List<Order> getOrders() {
return orders;
}
public void setOrders(List<Order> orders) {
this.orders = orders;
}
public static class Address {
@JsonProperty("city")
private String city;
@JsonProperty("postal_code")
private String postalCode;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getPostalCode() {
return postalCode;
}
public void setPostalCode(String postalCode) {
this.postalCode = postalCode;
}
}
public static class Order {
@JsonProperty("order_id")
private String orderId;
@JsonProperty("total")
private double total;
@JsonProperty("coupon")
private String coupon;
public String getOrderId() {
return orderId;
}
public void setOrderId(String orderId) {
this.orderId = orderId;
}
public double getTotal() {
return total;
}
public void setTotal(double total) {
this.total = total;
}
public String getCoupon() {
return coupon;
}
public void setCoupon(String coupon) {
this.coupon = coupon;
}
}
}Limits and caveats
- Needs the Jackson library (jackson-annotations) in your project.
- A number above 2147483647 in the sample makes the field long; otherwise it stays int.
Questions about this tool
Why Integer instead of int for some fields?
A primitive int cannot hold null, so Integer is used for a field that was null or missing in some items.
Do I need getters and setters?
Jackson reaches private fields through them. If you turn them off, the fields become public and the code still works.
Is my JSON sent to a server?
No. The conversion runs in your browser and the JSON never leaves your device.
