{}JSONLint.app

JSON to Java Converter

Generate Java POJO classes from JSON data. Paste your JSON and configure options:

Loading...
Loading...

Generate Java Classes from JSON

This tool automatically generates Java class definitions from your JSON data. It infers types from values, handles nested objects, and supports popular libraries like Lombok, Jackson, and Gson.

Output Options

Standard POJO with Lombok

Using Lombok's @Data annotation eliminates boilerplate by auto-generating getters, setters, toString, equals, and hashCode:

@Data
public class User {
    @JsonProperty("user_name")
    private String userName;
    
    private Integer age;
    private List<String> roles;
}

Java 16+ Records

Records are immutable data classes with concise syntax. Ideal for DTOs:

public record User(
    @JsonProperty("user_name") String userName,
    Integer age,
    List<String> roles
) {}

Jackson vs Gson

FeatureJacksonGson
Annotation@JsonProperty@SerializedName
SpeedFaster for large payloadsSimpler for small data
Spring BootDefault choiceRequires configuration

Type Inference

The converter maps JSON types to Java:

JSONJavaNotes
stringString
integerInteger / LongLong for values > 2.1B
decimalDouble
booleanBoolean
arrayList<T>Type from first element
objectNested classSeparate class generated
nullObject

Example Conversion

Input JSON:

{
  "id": 1,
  "username": "john_doe",
  "profile": {
    "firstName": "John",
    "age": 30
  }
}

Generated Java (with Lombok + Jackson):

package com.example.model;

import java.util.List;
import lombok.Data;
import com.fasterxml.jackson.annotation.JsonProperty;

@Data
public class Root {
    private Integer id;
    
    private String username;
    
    private Profile profile;
}

@Data
public class Profile {
    @JsonProperty("firstName")
    private String firstName;
    
    private Integer age;
}

Using Generated Classes

With Jackson

ObjectMapper mapper = new ObjectMapper();

// Deserialize
String json = getJsonFromApi();
Root data = mapper.readValue(json, Root.class);

// Serialize
String output = mapper.writeValueAsString(data);

With Gson

Gson gson = new Gson();

// Deserialize
Root data = gson.fromJson(json, Root.class);

// Serialize
String output = gson.toJson(data);

Best Practices

  • Use Lombok — Eliminates boilerplate getters/setters
  • Use wrapper typesInteger instead of intto handle nulls
  • Add validation — Consider adding @NotNull,@Size annotations manually
  • Use records for DTOs — Immutable, thread-safe, less code

Related Tools

Frequently Asked Questions

Should I use Lombok or write getters/setters?

Lombok is recommended for most projects — it reduces boilerplate significantly. However, some teams prefer explicit code for debugging or IDE support reasons.

When should I use Records vs Classes?

Use records for immutable data transfer objects (DTOs). Use classes when you need mutability, inheritance, or complex behavior.

How do I handle null values?

The converter uses wrapper types (Integer, Boolean) which can be null. For primitives that shouldn't be null, change toint, boolean manually.

Common Mistakes & Pro Tips

  • Whole numbers default to int and can overflowA JSON integer is typically mapped to `int`, but Java's `int` is 32-bit (max ~2.1 billion). A 64-bit ID or epoch-millis timestamp will overflow it, so promote those fields to `long` (or `Long`). For arbitrary precision use `BigInteger`, and for exact decimals like currency use `BigDecimal` instead of `double`.
  • Use wrapper types so null is possiblePrimitive types (`int`, `boolean`, `double`) cannot hold `null`, so a JSON field that may be absent or null must use the boxed wrapper (`Integer`, `Boolean`, `Double`). Jackson/Gson will throw or default a primitive when the JSON value is null, whereas a wrapper simply becomes `null`.
  • Map non-camelCase keys with @JsonProperty / @SerializedNameJava fields should be camelCase, but JSON often uses snake_case (`first_name`). Annotate the field with Jackson's `@JsonProperty("first_name")` or Gson's `@SerializedName("first_name")` so the renamed Java field still binds to the original key. Jackson can also do this globally via `PropertyNamingStrategies.SNAKE_CASE`.
  • Empty and mixed arrays lose their element typeAn empty array `[]` gives no element type and tends to become `List<Object>`; an array mixing objects of different shapes does the same. Replace `Object` with the real generic type once known, and remember Jackson needs `List<Foo>` to be a concrete parameterized type (or a `TypeReference`) to deserialize correctly.
  • Reserved keywords and dates need attentionJSON keys like `class`, `default`, or `package` are Java keywords and can't be field names — rename them and add `@JsonProperty` to map back. Date strings stay `String` because JSON has no date type; convert to `LocalDateTime`/`Instant` with `@JsonFormat` and the `jackson-datatype-jsr310` module if you want real date types.

Frequently Asked Questions

Should I generate plain POJOs, records, or Lombok classes?

Java 16+ `record` types are the most concise for immutable JSON DTOs and are supported by recent Jackson versions. Classic POJOs with getters/setters work everywhere and integrate with all frameworks. Lombok `@Data`/`@Builder` reduces boilerplate but adds a build-time dependency and annotation processor — choose based on your Java version and whether you want immutability.

How do I handle optional or nullable fields?

Use boxed wrapper types (`Integer`, `Long`, `Boolean`) so a missing or null JSON value maps to `null` rather than failing. Jackson ignores unknown/missing properties by default for missing keys (they stay null). To tolerate extra unknown keys, add `@JsonIgnoreProperties(ignoreUnknown = true)`. Avoid `Optional<T>` as a field type — Jackson supports it via a module, but it's unconventional for DTO fields.

Which deserialization library do these classes target — Jackson or Gson?

The generated POJOs are library-agnostic plain classes; the difference is only in the binding annotations. Jackson uses `@JsonProperty`/`@JsonIgnoreProperties`, while Gson uses `@SerializedName`. Pick the annotation set matching your stack — Spring Boot ships Jackson by default, while many Android projects use Gson or Moshi.

Why is a number field typed as `double` when I expected an integer?

If the sample value contained a decimal point (`19.99`) it's inferred as a floating-point type. If a field is conceptually an integer but the JSON shows `1.0`, change it to `int`/`long` manually. For monetary values prefer `BigDecimal` over `double` to avoid binary floating-point rounding errors.

How are deeply nested objects represented?

Each nested JSON object becomes its own static nested class or a separate top-level class, referenced by the enclosing class. Arrays of objects become `List<NestedType>`. You can usually choose between inner/nested classes and separate files; separate top-level classes are easier to reuse across DTOs.

Is my JSON sent to a server for conversion?

No. The conversion is performed entirely client-side in your browser, so the JSON never leaves your machine. This makes it safe for proprietary API schemas or data containing credentials.