Code Beautifier

JSON to Java

Generate Java records or POJOs from JSON with Jackson or Gson annotations, boxed types for nullable fields, and valid names for keys like class.

Draft saved locally.

Local workspace

Named projects in IndexedDB · Local only — never synced to our servers. Worksp

Open manager

Batch workspace

Format multiple files locally in one run.

Style:
Annotations:
json
Javajava

Mapping JSON to Java means choosing between records and POJOs, Jackson and Gson, and primitive and boxed types for every field. This generator makes those choices from the data. It merges every array item to find fields that can be missing or null and boxes them, keeps required values primitive, turns keys like first-name and class into valid Java names with @JsonProperty or @SerializedName, and puts the public root type first so the output compiles as a single file. Identical nested objects share one type.

Common errors and fixes

UnrecognizedPropertyException: Unrecognized field

Jackson found a key the class doesn't declare. Add the field, generate from a fuller sample, or annotate the class with @JsonIgnoreProperties(ignoreUnknown = true).

InvalidFormatException: Cannot deserialize value of type `long` from String

The payload sends a number as a string. Change the field to String, or enable coercion on the ObjectMapper.

Cannot map `null` into type `long`

A primitive field received null while FAIL_ON_NULL_FOR_PRIMITIVES is on. Use the boxed type Long, which the generator picks for nullable samples.

Expected BEGIN_OBJECT but was BEGIN_ARRAY

Gson's error when the JSON root is an array. Deserialize into List<Root> with a TypeToken.

Options

OptionDescription
StyleRecords are immutable and concise. POJOs suit older Java versions and frameworks that need mutable beans.
AnnotationsThe annotation that maps a renamed field back to its JSON key. Pick the JSON library your project already uses.
Root nameName of the top-level type. Nested types are named after their JSON keys.

FAQ

Should I generate a record or a POJO?

Records (Java 16+) are immutable and short, and Jackson 2.12+ and Gson 2.10+ deserialize them directly. Choose POJOs for older Java versions, for frameworks that need setters, or for JPA entities.

Jackson or Gson: which annotations should I pick?

Match the library your project uses. Spring Boot uses Jackson, so pick @JsonProperty. Many Android and older projects use Gson's @SerializedName. Choose None when your field names already match the JSON keys.

Why are some fields Long instead of long?

A primitive can't hold null and silently becomes 0 when a key is missing. Fields that are null or absent in any sample are generated as boxed types, so a missing value stays visible as null.

What happens to JSON keys like class or first-name?

They aren't valid Java identifiers, so they become class_ and firstName, and the annotation maps each field back to the original key.

How do I deserialize a JSON array at the root?

With Jackson, call readValue(json, new TypeReference<List<Root>>() {}). With Gson, pass new TypeToken<List<Root>>() {}.getType() to fromJson.