CruxDevCruxDev Tools Prompts
dev Utility • Zero-Server Privacy • 100% Client-Side

JSON → Java Class Generator

Generate Java POJOs or Records from JSON samples with proper types, getters/setters, and Jackson/Gson annotations.

Overview

Paste a JSON object and get a Java class skeleton whose field types are inferred from the actual values in your sample. The converter runs entirely in your browser tab, so the JSON never leaves the page, and it maps each JSON type to the closest Java type rather than flattening everything to Object.

How It Works

The tool opens with a small sample JSON already loaded, so the generated class is visible immediately. Replace the sample with your own object; the Java class regenerates on every keystroke. If the input is not valid JSON, or is a scalar or an array that is empty or holds non-objects, the output stays empty and a red error message explains the problem; an array of objects is accepted and described by one class. Click the output once to copy the whole class to the clipboard.

Step-by-Step Usage Guide

  1. Paste or edit the JSON object in the left input area.
  2. Review the Java class that appears in the output panel as you type.
  3. Adjust any types the generator could not infer, such as List<Object> for empty arrays or Object for null-only fields.
  4. Click the output area to copy the class into your project.

Technical Specifications & Standards

Type inference is value-driven. Strings become String, booleans become boolean, and numbers split on JavaScript's integer check: whole numbers map to long, numbers with a decimal point map to double, boxed as Long, Double or Boolean when a field can be null or is missing from some array elements. JSON null becomes Object, arrays become List<T> with the element types of all items merged (plus an import java.util.List line), and each nested object becomes its own package-private class named after its key, while only Root is public. Empty arrays become List<Object>, which compiles but needs a real type. Field names are kept when they are already valid Java identifiers, so a key like first_name stays first_name, which is legal Java but against most naming conventions; keys that are not valid identifiers (first-name, 2fa) are camelCased with a trailing // JSON key comment, and keywords get a trailing underscore. The generated class contains private fields only, with no getters, setters, or constructor, since it is a starting skeleton rather than a finished DTO. Integers larger than Number.MAX_SAFE_INTEGER cannot be held exactly in the browser's Number type, so they are typed double rather than long.

Targeted Use Cases

  • Scaffolding a DTO for a third-party REST API whose response you captured as JSON.
  • Comparing a Java model against a real payload during integration debugging.
  • Teaching material that shows how JSON types map onto Java types.
  • Quickly drafting a configuration class from a JSON settings file.

Notes & Gotchas

  • Feed the tool the richest sample you have, including a non-empty array, so element types are inferred.
  • Treat nullable API fields explicitly: null samples map to Object, so tighten the type yourself.
  • Rename snake_case fields to camelCase after copying, or add a serialization annotation.
  • Verify integers beyond 2^53 separately; the browser may have already rounded them.

Frequently Asked Questions

How are nested objects converted?

Each nested object becomes its own package-private class, named after its key, and the parent field uses that class. Only the Root class is public, so move the nested classes into their own files or make them static inner classes if your project requires it.

Why long instead of int for whole numbers?

JSON integers have no fixed width and JavaScript numbers represent integers safely only up to 2^53, and integers beyond that become double. Mapping to long is the conservative choice so typical API ids and timestamps fit without silently overflowing an int.

Does it generate getters and setters?

No. The output is a plain class with private fields. It is a starting skeleton; add accessors, a builder, or a record conversion yourself depending on your codebase style.

What happens with an array at the top level?

An array of objects is accepted: the elements are merged into one Root class (fields missing from some elements are boxed) with a comment saying it describes each element. Arrays of scalars, empty arrays and other non-object values are rejected with an error.