Utilo

JSON to Kotlin Converter – Free, Online, Private

Generate Kotlin data classes from a JSON sample, with kotlinx.serialization annotations and nullable fields where the sample omitted a key or used null.

Input
Output

How to convert JSON to Kotlin

  1. Paste a JSON object or array that resembles the payload you decode.
  2. Copy the data classes. Nested objects are their own classes, listed before the type that uses them.
  3. Add the kotlinx.serialization plugin if the project does not have it yet, then adjust nullability the sample could not prove.

What the data classes contain

Each JSON object becomes a @Serializable data class. Property names are camelCase Kotlin identifiers. When the JSON key is not that identifier, a @SerialName annotation keeps the wire name, so user_name becomes userName with @SerialName("user_name"). Integers become Long, other numbers become Double, booleans become Boolean, and strings stay String. Nested objects are named from the property, so address becomes Address. Arrays become List<T>. An empty array has no element evidence and becomes List<JsonElement>, which is the honest way to say the sample never showed a member. A key missing from some objects in an array is optional and defaults to null. A value that is null in the sample is nullable even if the key is always present.

Why Long and Double

JSON has one number type. The converter still splits whole numbers from fractional ones because Kotlin callers usually want a Long for an id and a Double for a price. A whole number that is actually a float in other responses will be wrong until you edit the property. JavaScript has already parsed the sample before this step, so an integer wider than 2^53 may have lost digits. Paste a string if the id is a snowflake. kotlinx.serialization does not coerce a JSON number into a String property, and this page will not pretend that it does. The generated class is a starting point you can compile, not a schema.

Worked example

The sample user becomes Root with id: Long, name: String, active: Boolean, tags: List<String>, and address: Address. Address has city and zip as String. tags is List<String> because both elements are strings. If one tag were a number, the element type would widen to a union the Kotlin generator represents as JsonElement, since Kotlin has no anonymous union in a List the way TypeScript does. There is no package declaration and no file name. Paste the classes into the package you already have. The constructor parameter order follows the JSON key order, which matters if you call the constructor without names.

What this page does not invent

Default values other than null are not guessed. Enums are not inferred from a handful of strings. Polymorphic type discriminators are not turned into sealed classes. A field that happened to be present is required, even if the API documents it as optional. If you need that optional, delete a key from one object in a sample array and convert again, or add ? = null by hand. The conversion runs in the browser. A customer payload can be turned into classes without posting it to a typing service. For Java records, Swift Codable, or Dart, use the sibling pages. They share the same inference and emit that language's syntax.

Frequently asked questions

Do I need kotlinx.serialization?

The annotations are from that library. Remove @Serializable and @SerialName if you decode with Moshi or Gson and keep the property names.

Why are some properties nullable?

Because a sample object omitted the key, or the value was null. A single complete object marks every key required.

Can the root be an array?

Yes. You get a typealias for the root and data classes for any nested objects.

Are unknown extra keys rejected?

The generated class does not set ignoreUnknownKeys. kotlinx.serialization's default is to fail on unknown keys unless you configure the Json instance.