JSON to Java Class Converter: Generate POJOs from JSON Data

Published: (February 22, 2026 at 12:38 PM EST)
2 min read
Source: Dev.to

Source: Dev.to

Introduction

Converting JSON responses to Java classes is one of the most common tasks in Java backend development. When consuming REST APIs, you need Java classes that match the JSON response structure. Manually writing POJOs for nested JSON objects is time‑consuming and error‑prone.

Sample JSON

{
  "id": 1,
  "name": "John Doe",
  "email": "john@example.com",
  "address": {
    "street": "123 Main St",
    "city": "Springfield",
    "zipCode": "62701"
  },
  "orders": [
    { "orderId": "A001", "total": 29.99 }
  ]
}

Generated POJO

Instead of writing classes by hand, use a JSON‑to‑Java converter to instantly generate:

  • POJO classes with private fields
  • Getters and setters for each field
  • Constructors (default + parameterized)
  • Nested classes for nested objects
  • List types for arrays
  • Proper Java types (String, int, double, boolean)
public class User {
    private int id;
    private String name;
    private String email;
    private Address address;
    private List orders;

    // Getters and setters
    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 Address getAddress() { return address; }
    public void setAddress(Address address) { this.address = address; }

    public List getOrders() { return orders; }
    public void setOrders(List orders) { this.orders = orders; }
}

Using Jackson for Serialization

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;

// Example POJO with Jackson annotations
@JsonIgnoreProperties(ignoreUnknown = true)
public class User {
    @JsonProperty("id")
    private int id;

    @JsonProperty("name")
    private String name;

    // other fields, getters, setters...
}

// Serialization / deserialization
ObjectMapper mapper = new ObjectMapper();
User user = mapper.readValue(jsonString, User.class);
String json = mapper.writeValueAsString(user);

Tips

  • Add @JsonProperty for fields whose JSON names differ from Java field names.
  • Use @JsonIgnoreProperties(ignoreUnknown = true) to ignore unexpected JSON properties and maintain forward compatibility.
  • For Java 16+ you can consider using records for immutable data carriers:
public record User(int id, String name, String email, Address address, List orders) {}
0 views
Back to Blog

Related posts

Read more »