formatjson.pro
Validate, pretty-print or minify JSON in your browser — nothing is uploaded.

JSON to Java Class

Paste a sample JSON object below and generate a Java `record` for it. The tool infers a type for every field and creates a named record for each nested object.

Runs entirely in your browser — your JSON is never uploaded to a server. No ads injected into the output, no sign-up. Generates modern Java records that Jackson and Gson deserialize without extra annotations.

How to generate a Java class from JSON

  1. Paste your JSON. Paste one representative JSON object (not an array) into the box above.
  2. Generate the record. Click Generate Java class. The tool infers a type for every field and emits a named record for the root and each nested object.
  3. Wire up a mapper. Copy the records into your project and deserialize with Jackson (ObjectMapper) or Gson; the field names already match the JSON keys.

How the type mapping works

Each JSON value maps to a Java type: a whole number becomes long, a decimal becomes double, a string becomes String, true/false becomes boolean, and null becomes Object. Arrays become List<T> with the element type boxed (Long, Double, Boolean) so the generic is valid.

Arrays and nested objects

An array becomes a typed collection using the type of its first element, and every nested object gets its own named type so the result compiles as-is. Uses Java 17+ records — compact, immutable, and understood by Jackson and Gson out of the box.

Using the result

Jackson or Gson can deserialize into these records directly: new ObjectMapper().readValue(json, Root.class).

FAQ

Why records instead of plain classes?

Records give you an immutable class with a constructor, accessors, equals, hashCode and toString in one line. If your framework needs mutable POJOs with getters/setters, convert each record into a class — the field names and types are already correct.

Why is a number typed as long, not int?

JSON has a single number type. The tool guesses long for whole numbers to avoid overflow on large ids, and double for decimals. Narrow to int or BigDecimal where you know the real range.

Does one sample produce a complete class?

It produces a correct starting point. A single sample cannot reveal fields that are optional or null in other responses, so check the record against more examples and make nullable fields boxed types.