Generate Java Models from a JSON Sample
Use this JSON to Java converter to create a first draft of Java records or classes from an example payload. It is useful when you need model types for an API response, configuration file, webhook, or test fixture and want to avoid typing every field by hand. The output reflects the sample you provide, so treat it as a starting point and compare it with the API contract before using it in an application.
Paste JSON or open a local JSON file, choose a root type name and optional package, select records or classes, and generate the code. The converter infers nested object types and Java lists. You can also add Jackson @JsonProperty annotations for renamed fields and choose BigDecimal for decimal-valued properties. The conversion runs in your browser; the input is not sent to a conversion service.
How to Convert JSON to Java
- Paste a valid JSON object or array, or select Open JSON to load a file.
- Enter a root record or class name, such as
Person. Use a valid Java identifier. - Choose Java records or Java classes. Add a package name such as
com.example.modelsif your project uses one. - Choose whether to include the sample-construction code and Jackson property annotations. Enable
BigDecimalif you want decimal fields represented with that type. - Select Generate Java. Review the imports, field types, nested models, and generated names before copying or downloading the source.
Example: JSON to Java Record
For this sample, set the root name to Person and leave the output style set to records:
Input JSON
{
"id": 101,
"name": "Ada Lovelace",
"active": true,
"profile": { "city": "London" },
"skills": ["mathematics", "logic"]
}
Generated model types
import java.util.List;
record Profile(String city) {}
record Person(int id, String name, boolean active,
Profile profile, List<String> skills) {}
The converter creates a separate type for the nested profile object and uses List<String> for the skills array. If sample construction is enabled in record mode, the output also includes example code that creates the record from the supplied values.
Records or Classes: Which Output Should You Choose?
Choose records for compact data-carrier types when your project targets Java 16 or newer. Record components are final, and the generated sample code can show how to construct one. In this converter, record declarations are package-private; add public yourself if the type needs to be accessed from another package.
Choose classes when you need mutable fields or are working with a codebase that uses ordinary classes. The generated root class has public fields and nested object types. It does not add custom constructors, getters, setters, or a JSON deserializer. A missing property and an explicit null can both leave a reference field null, so add validation if your application needs to tell those cases apart.
How JSON Values Map to Java Types
| JSON example | Typical generated Java type | Review point |
|---|---|---|
"Ada" | String | Date-like strings remain strings. |
true | boolean or Boolean | Records use the primitive when always present and non-null; nullable or missing sample fields use the wrapper. |
101 | int/Integer or long/Long | Inferred from the sample number and whether it fits the integer range. |
12.75 | double/Double or BigDecimal | Enable the BigDecimal option for decimal fields, then inspect precision. |
null | Object or a nullable reference type | Java has no direct equivalent of a nullable primitive. |
["math", "logic"] | List<String> | Empty or mixed arrays can be inferred less specifically. |
{ "city": "London" } | A nested record or class | Nested type names are derived from their JSON property names. |
Optional Fields, Nulls, and Different Sample Shapes
JSON can omit a property or include it with the value null; those cases are not the same. When the input is an array of objects, the converter merges their fields. In record mode, a field missing from any sample record or set to null uses a reference type such as Integer or Boolean instead of a primitive. Java does not have an optional-property marker like TypeScript’s ?, so check how your application should represent absent values.
One example object cannot reveal fields that may appear in other responses. Include several representative records if the shape varies. If your API has distinct response variants, consider modeling those variants explicitly rather than relying on one merged sample. Class output also does not turn missing sample properties into optional fields; inspect primitive fields because their default values may conceal missing input.
Jackson Annotations and Package Names
Java identifiers cannot use every JSON property name. For example, the key first-name becomes the Java field firstName. With Jackson annotations enabled, the converter adds @JsonProperty("first-name") so Jackson can map the original key to that field. The annotation option only adds annotations where a JSON key needs a different Java identifier. Your project must include the Jackson annotations dependency, and annotations alone do not generate serialization or deserialization code.
The optional package field adds a package declaration at the top of the output. Make sure it matches your project’s package structure and that the source file is placed in the corresponding directory.
Check Number Precision and Empty Arrays
Whole numbers are inferred as int or long when possible. Decimal numbers become double by default, or BigDecimal when that option is selected. Because JSON numbers are parsed in the browser before code generation, very large integers or high-precision decimals may already be rounded. Check the generated value if exact precision matters, especially for financial values or numeric identifiers.
An empty array does not show what its element type should be, so it becomes List<Object> unless other samples provide an element type. Mixed arrays and values with incompatible shapes may also fall back to Object. Replace broad types with the domain model you actually expect.
Generated Java Is Not Runtime JSON Validation
The converter parses the example and writes model declarations. It does not check future responses, enforce business rules, or automatically deserialize JSON into the generated types. Connect the models to a JSON library such as Jackson using your project’s configuration, and add validation when incoming data must meet required constraints. For a machine-readable JSON contract, try the JSON Schema Generator and review the inferred required fields and constraints.
Frequently Asked Questions
Which Java version do generated records require?
Java records require Java 16 or newer. Choose class output if your project targets an earlier Java version.
Can this converter generate Java classes instead of records?
Yes. Select Java classes to generate a mutable root class with nested classes for object values.
How are nested JSON objects represented?
Nested objects become separate named record or class types. Their names are derived from the JSON property names, and the parent model refers to them by type.
How are arrays converted?
Arrays become List<T> fields when the converter can infer an element type. An empty array has no item example and falls back to List<Object>.
Do I need Jackson to use the generated code?
No. Jackson annotations are optional. If you enable them, your project needs the Jackson annotations dependency. The generated code does not include a JSON mapper or deserializer.
Does the converter send my JSON to a server?
No. The page parses the input and generates Java in your browser. For sensitive data, follow your organization’s handling rules and review the privacy information.
Related JSON Tools
Check source syntax with the JSON Validator, make the sample easier to read with the JSON Formatter, or inspect nested values in the JSON Viewer. If your project uses other model formats, try the JSON to TypeScript Converter, JSON to Python Converter, or JSON to SQL Converter.