JSON to Java Class Generator
Paste a JSON payload and this JSON to Java converter walks the structure and writes matching Java classes in your browser. Use it to convert JSON to Java classes with the style, annotations, and field types your project already runs, then paste the result straight into your editor. Nothing is uploaded: the text stays in this tab's memory and disappears when you close it.
How the converter works
The generator parses your JSON, walks the root, and picks a Java type for every key. Whole numbers inside the 32-bit range become int, larger whole numbers become long, and decimals become double unless the BigDecimal checkbox is on, which swaps them for BigDecimal and adds the java.math.BigDecimal import. true and false become the primitive boolean. Strings stay String unless the date box is set to java.time and the value matches an ISO pattern, in which case it becomes LocalDate, LocalDateTime, or OffsetDateTime with the matching import written at the top of the file.
A nested object becomes a static nested class inside its parent class, named after the key in PascalCase by the same routine that seeds the class-name box, so an address key produces class Address. An array of objects produces one class named from the singular form of the key: a tags array of strings stays a List of String, while a products array of objects yields a Product class and a List of Product field. Elements inside an array are merged before the type is picked. All whole numbers give Integer or Long, all decimals give Double or BigDecimal, all booleans give Boolean, and all strings give String. Mixed element types, arrays inside arrays, and a root that is itself an array all produce a warning next to a safer fallback such as List of Object.
A null value has no type to read, so the field becomes Object and a warning names it. Keys that collide with Java keywords get a trailing _ character: class becomes class_. Keys with dashes or other punctuation are camel-cased, so user-name turns into userName, and a key that starts with a digit is prefixed, so 123start becomes _123start. Choose Gson or Jackson in the annotations box and each renamed field also carries @SerializedName or @JsonProperty holding the original key, so round-trip serialization still matches the wire format. Leave annotations off and the warnings list every rename instead. The package box emits a package line at the top; an invalid package is repaired by lower-casing each segment and stripping disallowed characters, with a warning that shows the replacement, or dropped when nothing usable remains.
Examples
Both examples below use the default POJO style with private fields and no annotations.
{"user": {"id": 42, "firstName": "Ada", "isActive": true, "score": 9.5, "address": {"city": "London", "zip": "SW1A"}}, "tags": ["math", "code"], "createdAt": "2026-10-20T12:00:00Z"} | JSON | Java field | Why |
|---|---|---|
| user, an object | User user | Static nested class User, named from the key |
| user.id = 42 | int id | Whole number inside the 32-bit range |
| user.firstName = "Ada" | String firstName | Plain string with no date pattern |
| user.isActive = true | boolean isActive | Primitive boolean with is and set methods |
| user.score = 9.5 | double score | Decimal number; BigDecimal when the checkbox is on |
| user.address, an object | Address address | Second nested class holding city and zip |
| tags = ["math", "code"] | List<String> tags | String elements merge to a single list type |
| createdAt = "2026-10-20T12:00:00Z" | String createdAt | OffsetDateTime when the date box is set to java.time |
{"class": "Vehicle", "user-name": "Ada", "123start": true, "nickname": null} | JSON key | Java field | Note |
|---|---|---|
| class | String class_ | Keyword renamed with a trailing _ character |
| user-name | String userName | Dashes are removed and the next letter is capitalized |
| 123start | boolean _123start | A leading digit gets a _ prefix |
| nickname | Object nickname | Null has no type, so Object is assumed with a warning |
When to use this tool
- REST DTOs. A teammate pastes a sample response from a new endpoint and you need request and response classes before integration work starts.
- Retrofit and Feign models. Android and Spring clients need model classes that map onto the exact JSON the server already sends, annotations included.
- Copying an API response shape. Third-party services rarely ship a Java SDK, so the sample payload is often the only description of the structure you have.
- Test fixtures. JUnit tests that post JSON bodies need matching classes, and generating them from the fixture keeps the test data and the types in step.
- Legacy interop. Older systems export keys like class and package; the rename handling plus Gson or Jackson annotations keeps the wire format while the Java side stays legal.
- Reading an unknown payload. Paste the document, read the generated class tree, and you have a readable map of the structure before writing any hand-made code.
- Teaching sessions. Showing the same JSON turn into compilable Java in front of a class is quicker than typing the file by hand.
Best practices
- Read every warning before pasting the class into a project. Each one marks a guess the generator had to make, such as Object for a null or List of Object for a mixed array.
- Use BigDecimal for money. A double cannot hold 0.1 exactly, so prices, balances, and rates should come out of the decimal checkbox as BigDecimal.
- Match the style to the project. A Spring service with Lombok on the classpath wants the Lombok style, a Java 17 codebase that favors immutability wants records, and everything else gets a POJO.
- Keep the package line in step with the folder. com.example.billing belongs under src/main/java/com/example/billing, and moving one without the other breaks the build.
- Add the annotation library to the build. Gson annotations need the Gson artifact and Jackson annotations need jackson-annotations, or the class will not compile.
- Rename the root class to something domain-specific before committing. OrderDto reads better in review than Root.
- Re-generate after the API changes. A new key in the payload is a new field in the class, and diffing two runs of the converter is a quick way to see what moved.
Common mistakes
- Trusting field types from one sample. A key that holds a number in one response may arrive as a string in another, so check the API's documented type.
- Forgetting nullability. Payloads often omit optional keys, and a primitive int fails when the key is missing; widen the field to Integer or set a default in your mapper.
- Using double for money. 0.1 plus 0.2 does not equal 0.3 in floating point, which is the kind of drift finance code cannot absorb.
- Adding annotations without the library. A class carrying @JsonProperty does not compile on a classpath that lacks jackson-annotations.
- A package name that does not match the folder layout. The compiler rejects a class whose package line disagrees with its directory.
- Shipping renamed fields without annotations against a live API. Without @SerializedName or @JsonProperty, a field named userName never binds back to user-name on the wire.
- Pasting a Lombok class into a project without the processor. @Data is invisible to javac unless Lombok is configured as an annotation processor in the build.
Frequently asked questions
Does my JSON leave the browser?
No. The conversion runs entirely in this page's JavaScript, inside your browser's memory. The text you paste is never uploaded, no request carries it, and nothing is saved to storage. The only network activity the page produces is the normal download of its own files. Closing the tab discards everything.
What Java types do numbers and booleans get?
Whole numbers inside the 32-bit range become int, and whole numbers outside it become long. Decimal numbers become double by default, or BigDecimal when the decimal checkbox is on, which is what money fields should use. Booleans become the primitive boolean in a class field. Inside a List, the boxed forms appear instead: Integer, Long, Double, BigDecimal, and Boolean, because list elements may hold null.
How are nested objects and arrays of objects handled?
A nested object becomes a static nested class inside its parent class, named after the key in PascalCase. An array of objects produces a single class named from the singular form of the key, so a products array yields a Product class and a List of Product field. When array elements disagree on type, the field falls back to List of Object and a warning appears, so the mismatch is visible before you paste the code anywhere.
What happens to keys that are Java keywords or contain dashes?
A keyword such as class gets a trailing _ character and becomes class_. Keys with dashes or punctuation are camel-cased, so user-name turns into userName, and a key that starts with a digit is prefixed, so 123start becomes _123start. Choose Gson or Jackson in the annotations box and each renamed field also carries @SerializedName or @JsonProperty with the original key inside quotes, so serialization still matches the wire format. Leave annotations off and the warnings list every rename.
POJO, Lombok, or record: which style fits my project?
POJO writes plain fields with getters and setters and runs on any Java version. Lombok writes a @Data annotation on the class, and it needs the Lombok library on the classpath plus an annotation processor configured in the build. Record writes a compact immutable Java record and needs Java 16 or newer. Pick the style the target project already uses so the generated file does not stick out.
Can date strings become java.time types?
Yes. Switch the date box to java.time and any string matching an ISO date, local datetime, or datetime with an offset is mapped to LocalDate, LocalDateTime, or OffsetDateTime, with the matching import added at the top of the file. Strings that do not match those patterns stay as String. A warning lists every field the date mapping touched.
Can I trust the field types from one sample payload?
Treat them as a starting point. One payload shows only the types that happened to appear in it. A null field becomes Object with a warning, and a key that is missing from the sample is missing from the class. Compare the result against the API's documented contract and widen primitives to their boxed forms where the server may omit a field.
Related tools
How to convert JSON to a Java class
- Paste JSON or load a preset Drop a JSON object or an array of objects into the input box, or press a preset button to load an example that already works.
- Set the class name, package, and style Choose a root class name, an optional package, the POJO, Lombok, or record style, and whether renamed fields get Gson or Jackson annotations.
- Copy the generated Java Read any warnings under the output, then press Copy to put the generated Java classes on the clipboard.