JSON to Kotlin Data Class
Generate Kotlin data classes from a JSON sample, for Android and backend Kotlin projects. Nested objects become their own data classes, arrays become List types, and whole numbers become Int unless a value is too large, in which case the field becomes Long. Turn on kotlinx.serialization to get @Serializable classes with @SerialName wherever a property name differs from the JSON key.
Result will appear hereHow to use the JSON to Kotlin Data Class
- Paste a JSON sample in the left pane.
- Set the root class name and package, and choose whether kotlinx.serialization annotations are included.
- Press Generate, then copy the classes or download them as a .kt file.
Example
Whole numbers stay Int here; a value past 2.1 billion would become Long.
{
"name": "json-utils",
"version": "1.0.0",
"private": false,
"tags": ["json", "tools"],
"author": { "name": "Ada", "active": true },
"score": 9.5,
"notes": null
}Frequently asked questions
When does a field become Long instead of Int?
When any value seen for that field falls outside the 32 bit Int range. One large value is enough, because the smaller type would overflow at runtime.
What does the nullable option change?
With nullable fields on, values the sample shows as null, and keys missing from some array elements, become nullable types such as String?. With it off, fields use the non nullable type of the non null values.