JSON to Java Converter
Java POJOs from a JSON sample, in one compilable file
Input
Output
What does the JSON to Java converter generate?
Mapping an API response onto Java usually means a pile of POJOs, each with fields, getters and setters. Paste a JSON sample and this page writes them for you as a single RootObject.java: your package line, the imports it needs, and a public root class with every nested object as a public static class inside it. Because it's one file, it compiles as is; the output is checked with javac.
Field names are camelCase. When a key isn't a legal Java name, such as first name, billing_cycle or class, the field is renamed (firstName, billingCycle, class_) and gets a Jackson @JsonProperty("first name") annotation so deserialization still finds the key. Tick Use Lombok to replace the getters and setters with @Data. If you use Gson instead, swap the annotation for @SerializedName.
Integers become Integer, Long once a value passes 32 bits, and BigInteger past the signed 64-bit range. Arrays become List<T>, nested arrays List<List<T>>. Every array element is inspected, so keys that only some objects have are still included. Wrapper Types (on by default) uses Integer, Double and Boolean everywhere so any field can hold null; turn it off to get int, double and boolean wherever the sample never had a null there. All of this runs in your browser.
How to use it
Paste JSON and the class updates as you type.
Paste a JSON sample
Paste into the Input panel, Upload a .json or .txt file, or click Sample. Example input:
{"subscriberId":"SUB-1001","plan":"Unlimited 5G","dataUsage":45.2,"status":"active"}Set Class Name and Package (invalid characters are dropped from the package). Not sure the JSON is valid? Try the JSON Validator first.
Check the output
The Output panel shows RootObject with Account, Device and Subscriber nested inside it for the sample. Prefer one class per file? Move each static class out and drop the static keyword.
Copy or download
Copy the code or Download it. Then read it with Jackson: new ObjectMapper().readValue(json, RootObject.class).
Where this saves time
Spring Boot request and response bodies
Paste the payload a client sends you and get the classes for your @RequestBody parameter. Spring Boot uses Jackson by default, so the @JsonProperty renames work without extra setup.
Calling external APIs
Capture a response from the API you integrate with and generate typed classes for RestTemplate, WebClient or a plain HttpClient plus ObjectMapper.
Android models
Generate plain classes for Retrofit with a Jackson converter, or switch the annotation to Gson's @SerializedName if your app uses Gson.
Spotting 64-bit IDs
Order numbers and epoch-millisecond timestamps overflow an int. The converter checks every value, so those fields come out as Long instead of failing at runtime.
Common Questions
Which JSON library does the output target?
The classes are plain Java and work with any library that maps fields or getters. Renamed keys use Jackson's @JsonProperty, so with Jackson they work as generated. With Gson, replace it with @SerializedName.
Why is everything in one file?
Java allows only one public top-level class per file (see the Java Language Specification), so the nested types are public static classes inside the root class. That way the download compiles immediately. You can split them into separate files later.
What does Wrapper Types do?
On: every number and boolean uses a wrapper type (Integer, Double, Boolean), so a missing value is null. Off: fields use primitives unless the sample had a null there or the key was missing from some objects.
How are null values typed?
A key that was only ever null becomes Object, since the sample can't tell what it holds. A key that is sometimes null and sometimes a string becomes String.
What happens with a root array?
The objects inside it become the root class and a comment shows the List<RootObject> to read into. A root that is null, a string or an array of plain values gives a short message instead of code.
Is my JSON sent to a server?
No. The classes are generated by JavaScript in your browser; nothing is uploaded. Input is capped at 256 KB.
Other JSON Tools You Might Need
On Kotlin or Scala instead, or want to check the JSON first?