Xerobit

JSON to Kotlin Data Class Converter

Paste a JSON object or array and get Kotlin data classes ready to copy. This json to kotlin data class converter infers Int, Long and Double, merges array elements into one class, marks nullable properties, and runs entirely in your browser.

A JSON object or array. A root array becomes a typealias for List of the merged element class.

Marks every property nullable with a null default. Properties that are null or missing in some objects of an array are nullable already.

Adds @Serializable to every class and a fromJson function on the root class, using kotlinx.serialization.

Kotlin Data Classes

data class Data(
    val name: String,
    val age: Int,
    val active: Boolean
)

By · Published · How we build and test tools

How to use the json to kotlin data class converter

  1. Paste your JSON into the input area. The tool accepts a JSON object or an array at the root.
  2. Set the root class name (default is Data) and optionally a package name.
  3. Choose whether to make all properties nullable and whether to generate a companion object with a fromJson method.
  4. Copy the generated Kotlin code from the output area.

How the conversion works

The converter reads your JSON with the built-in JSON.parse and walks the tree. Each object becomes a Kotlin data class. The type of each property is inferred from the sample values:

  • JSON strings become String and booleans become Boolean.
  • Whole numbers become Int, or Long if any value is beyond the Int range (about 2.1 billion). Numbers with a fraction become Double, and a property that mixes whole and fractional values is a Double.
  • null on its own, and the contents of an empty array, become Any? because the type cannot be known from the sample.
  • Arrays become List<T>. All elements are merged, so an array of objects gives one class that covers every key seen; arrays mixing unrelated kinds become List<Any>.
  • Objects become nested data classes named after the key in PascalCase (a key items gives Item).

If two classes would share a name, the parent name is put in front and then a number is added. Keys that are Kotlin keywords, such as class, are kept as the property name in backticks. Keys that are not valid identifiers (such as user-id or 1st) become camelCase properties with a @SerialName("user-id") annotation so the original key is not lost. An empty object becomes a plain class, because a Kotlin data class needs at least one property.

Nullable types and the <code>useNullable</code> option

A property is nullable when its value is null or the key is missing in at least one object of an array. For example [{"a":1},{"a":null,"b":"x"}] gives val a: Int? = null and val b: String? = null. Properties missing from some objects also get a = null default, so deserialising does not fail.

Enable Make All Properties Nullable to add ? and a = null default to every property. This helps when the API may omit fields that your sample always contained: with the option off {"age":25} gives val age: Int, and with it on you get val age: Int? = null.

Companion object with <code>fromJson</code>

When Generate Companion Object with fromJson is checked, every class gets the @Serializable annotation and the root class gets a companion object with fromJson(json: String), which uses Json { ignoreUnknownKeys = true } from kotlinx.serialization. You need the Kotlin serialization plugin and the kotlinx-serialization-json dependency in your project.

kotlinx.serialization has no serializer for Any, so with this option untyped values are written as JsonElement instead. A root array becomes a typealias for a list, and you decode it with Json.decodeFromString directly, so no companion is generated for it.

The option is off by default because not every project uses kotlinx.serialization; the plain data classes work as the model for Gson, Moshi or Jackson when the property names match the JSON keys, and for renamed properties you swap @SerialName for @SerializedName (Gson) or @Json(name = ...) (Moshi).

Reference: common JSON patterns and their Kotlin output

JSONKotlin (default options)
{"id":1,"name":"A"}data class Data(val id: Int, val name: String)
{"items":[1,2,3]}data class Data(val items: List<Int>)
{"big":3000000000}data class Data(val big: Long)
{"price":1.5}data class Data(val price: Double)
{"user":{"login":"bob"}}data class Data(val user: User) and data class User(val login: String)
{"tags":["a",1]}data class Data(val tags: List<Any>) (mixed array)
{"empty":[]}data class Data(val empty: List<Any?>)
{"user-id":1}data class Data(@SerialName("user-id") val userId: Int)

Common mistakes and edge cases

  • Invalid JSON - the tool shows the JSON.parse error message. Fix the JSON before converting.
  • Treating one sample as the schema - a value that is 5 in your sample may be 5.5 in other responses. Merging array elements helps, but check the API documentation for the real types.
  • Root array - a root array becomes typealias Name = List<NameItem> plus a data class for the elements, not a wrapper property.
  • Duplicate names - keys such as user_info and userInfo can produce the same class name; the second one gets the parent name or a number added.
  • Large numbers - JSON numbers are read as doubles, so integers beyond 2^53 lose precision before the type is chosen. Send exact 64-bit IDs as strings.

Worked examples

ExampleInputResult
Simple ObjectJSON Input: {"name":"Alice","age":30,"active":true}; Root Class Name: Userdata class User( / val name: String, / val age: Int, / val active: Boolean / )
Nested ObjectJSON Input: {"user":{"name":"Bob","age":25},"tags":["admin","user"]}; Root Class Name: Responsedata class Response( / val user: User, / val tags: List<String> / ) / data class User( / val name: String, / val age: Int / )
Array of ObjectsJSON Input: [{"id":1,"value":"foo"},{"id":2,"value":"bar"}]; Root Class Name: ItemListtypealias ItemList = List<ItemListItem> / data class ItemListItem( / val id: Int, / val value: String / )

FAQ

Does the tool send my JSON to a server?

No. The conversion runs entirely in your browser using the built-in JSON parser. Nothing is uploaded.

Can I convert a JSON array at the root?

Yes. A root array becomes a typealias for List of the element class, for example typealias ItemList = List<ItemListItem>, and all elements are merged into one class.

How does it choose between Int, Long and Double?

Whole numbers within the Int range become Int, larger whole numbers become Long, and anything with a fractional part becomes Double. If a property holds both whole and fractional values in the sample, it becomes Double. A single sample can still miss values, so confirm against the API documentation.

What if a property is sometimes an object and sometimes null?

If both appear in the sample (for example in different array elements), the property becomes a nullable class type such as User?. If only null appears, the type is Any? because nothing shows what the object contains.

Do I need kotlinx.serialization?

Only if you enable the companion object option, which adds @Serializable and a fromJson function. Renamed properties use @SerialName from the same library; for Gson or Moshi swap that annotation for their equivalent.

How are duplicate class names handled?

When two keys produce the same PascalCase name, the parent class name is put in front, and a number is added if there is still a clash. Names that would hide Kotlin types, such as a key called string, get Model added.

How large a JSON document can I convert?

Up to 2 MB. Larger input is rejected with a message; for huge files use a command-line tool or IDE plugin.

How does the json to kotlin data class converter handle nested objects?

It creates a separate data class for each nested object, named after the key in PascalCase, and references it from the parent class. Nesting deeper than 100 levels is rejected.