JSON to Kotlin
Generate Kotlin data classes from JSON for kotlinx.serialization, Moshi, or Gson, with nullable defaults for optional fields and backticks for keywords.
Local workspace
Named projects in IndexedDB · Local only — never synced to our servers. Worksp…
Batch workspace
Format multiple files locally in one run.
Kotlin's type system distinguishes nullable from non-null values, but a JSON sample doesn't say which fields can be absent. This generator merges every item of an array to find properties that are missing or null and makes them nullable with a null default, which kotlinx.serialization and Moshi need to decode partial data. It converts snake_case keys to camelCase properties with @SerialName, @Json, or @SerializedName, escapes keywords like when with backticks, and escapes $ in keys so the annotations compile. Identical nested objects share one data class.
Common errors and fixes
MissingFieldException: Field 'email' is required for type with serial name 'Root', but it was missing
The key isn't in the data. Include a sample without it so the property is generated as nullable with a null default.
Unexpected JSON token at offset: Encountered an unknown key
kotlinx.serialization rejects keys the class doesn't declare. Add the property, or decode with Json { ignoreUnknownKeys = true }.
Serializer for class 'Root' is not found
The class isn't marked @Serializable, or the kotlinx.serialization compiler plugin isn't applied in the Gradle build.
Required value 'email' missing at $
Moshi's error for a missing non-null property. Make the property nullable with a null default, as the generator does for optional samples.
Options
| Option | Description |
|---|---|
| Library | Sets the class and property annotations, and the types used for free-form objects and mixed values. |
| Root name | Name of the top-level data class. Nested classes are named after their JSON keys. |
FAQ
kotlinx.serialization, Moshi, or Gson: which should I use?
kotlinx.serialization is the official, multiplatform option and generates serializers at compile time. Moshi understands Kotlin nullability and is common on Android. Gson uses reflection, ignores Kotlin's nullability and default values, and can put null into a non-null property.
Why do optional properties have = null?
kotlinx.serialization throws MissingFieldException for a missing key unless the property has a default. Keys that are absent from some samples become nullable with a null default, so the data still decodes.
Why is a property wrapped in backticks, like `when`?
when is a hard keyword in Kotlin, and backticks make it a valid property name. The @SerialName annotation keeps the mapping to the JSON key explicit.
Why is a property typed JsonObject or JsonElement?
With kotlinx.serialization, an empty object in the sample becomes JsonObject and a key with mixed types becomes JsonElement. The Moshi and Gson outputs use Map<String, Any?> and Any instead.
How do I parse a JSON array at the root?
With kotlinx.serialization, call Json.decodeFromString<List<Root>>(text). The generator also writes a typealias such as RootList for the array type.