Skip to content
Devtoolry

JSON to Java

Generate Java records or classes with getters and setters from JSON online, with Jackson @JsonProperty annotations and List types for arrays.

Something not working, or missing a feature? Send feedback

About the JSON to Java

The JSON to Java converter generates Java records (Java 16+) or classic classes with getters and setters from a JSON sample. Arrays become List types, nested objects become their own types, and Jackson @JsonProperty annotations are added where a Java name differs from the JSON key.

Conversion happens in your browser, so your files and records stay private.

How to use the JSON to Java

  1. Paste a JSON sample and set the root type name.
  2. Choose records or classes, and whether to add Jackson annotations.
  3. Copy the code. Only the root type is public, so move other types into their own files if your project needs one public type per file.

Example

Java records from a user with projects

Input

{
  "id": 7,
  "fullName": "Grace Hopper",
  "email": null,
  "roles": ["admin"],
  "team": { "name": "Compilers", "size": 4 },
  "projects": [
    { "name": "COBOL", "year": 1959 },
    { "name": "A-0", "year": 1952, "archived": true }
  ]
}

Result

import java.util.List;

public record User(
    long id,
    String fullName,
    Object email,
    List<String> roles,
    Team team,
    List<Project> projects
) {}

record Project(
    String name,
    long year,
    Boolean archived
) {}

record Team(
    String name,
    long size
) {}

archived is missing from one project, so it is a boxed Boolean that can be null. Records need Java 16+; switch the style to class for older versions.

Common mistakes

  • Primitives for nullable values. A long or boolean cannot be null, so a missing value silently becomes 0 or false. Keep the boxed types the converter chose for fields that were null or missing.
  • Several public types in one file. Java allows one public top-level type per file. Move each type into its own file, or nest them as static classes inside the root type.

Works well with the JSON to Java

Frequently asked questions

Does this work with Jackson and Gson?

Yes. Jackson reads both records and classes; the @JsonProperty annotations cover keys like snake_case names. Gson reads the classes too; replace @JsonProperty with @SerializedName if you use Gson.

Why are some fields Long or Boolean instead of long or boolean?

Fields that were null or missing in the sample use boxed types, which can hold null. Primitives cannot, and would silently become 0 or false.

Should I use records or classes?

Records are shorter and immutable, and Jackson supports them since 2.12. Use classes for Java versions before 16, or when a framework needs a no-argument constructor and setters.