JSON to Java Class Converter
FV-16Paste a JSON sample and press Convert to get Java classes with private fields, getters and setters, or records. Jackson @JsonProperty annotations keep the original keys. It runs in your browser.
Runs in your browser. Nothing is uploaded.
JSON
Java
How to use it
- Paste a JSON sample into the input box, or open a .json file.
- Choose a class with getters and setters, or a record for Java 16 and later.
- Press Convert and copy the code into your project.
- Read it with new ObjectMapper().readValue(json, Root.class) if you use Jackson.
Classes or records
The class style writes private fields with a getter and setter for each, the usual shape for Jackson and other binders. The record style writes one compact record per object, with annotations on the components. In both cases the types are nested inside the first class, so a single file compiles as pasted. Move them into separate files if your project keeps one public type per file.
Field names and annotations
Field names are camelCase. When the name differs from the JSON key, the field or record component gets @JsonProperty with the original key, which needs the Jackson annotations on the class path. Java keywords such as class get a trailing underscore. A boolean field with a primitive type uses an is getter; other fields use get.
Types and what they cannot know
Required fields use primitives (int, long, double, boolean). A key that is null in some items or missing from some objects uses the boxed type, such as Integer, so it can hold null. A whole number beyond long becomes BigInteger, and mixed types, null-only keys and empty arrays become Object. The tool only sees your sample, so check the types against the API documentation.
Questions people ask
How do I convert JSON to a Java class?
Paste the JSON, choose the class style, and press Convert. Copy the result into your project and read the JSON with a mapper such as Jackson: new ObjectMapper().readValue(json, Root.class). Add the Jackson dependencies first.
When should I choose records?
Choose records on Java 16 or later when the data is read-only, since records have no setters. Pick the class style for older Java versions or when you want to change fields after loading.
Why do some fields use Integer and others int?
A primitive int cannot hold null. Fields that are always present and never null in your sample use primitives. A key that is null in some items, or missing from some objects, uses the boxed type, which can be null.
How are numbers mapped?
Whole numbers become int, or long beyond the 32-bit range. Anything with a decimal point or exponent becomes double. A whole number beyond long becomes java.math.BigInteger, which keeps every digit.
Do I need Jackson to use the output?
Only when a field name differs from its key, because then the code has @JsonProperty from Jackson. If every key already matches a camelCase name, no annotation is written and any binder that maps by name can use the class.
What happens when the JSON starts with an array?
The first class is named after the items, for example RootItem, and a comment at the top says the document is a List of that type. Read it as a list of those objects.
About this tool
Infers types from the sample you paste, so it only knows what that sample shows. Field names are camelCase and the original key goes in a Jackson @JsonProperty annotation whenever the name differs, so you need Jackson annotations on the class path. Keywords such as class get a trailing underscore. Required fields use the primitive types (int, long, double, boolean); a key that is null in some items or missing from some objects uses the boxed type (Integer, Boolean) so it can hold null. Whole numbers become int, or long beyond the 32-bit range, and anything with a decimal point or exponent becomes double. A whole number beyond long becomes BigInteger. Arrays become List<T>; mixed types, null-only keys and empty arrays become Object. All types are written nested inside the first class, so one file compiles as pasted; move them out if you prefer one file per type.