PasteZap
🌐 English
Free · No account

FREE · DEVELOPER TOOLS

JSON to Kotlin Data Class.

Turn a JSON sample into Kotlin data classes for kotlinx.serialization, with @SerialName for keys that are not camelCase and nullable optional fields.

Limits & supported formats

Types are inferred from the sample only. Everything is calculated in your browser; nothing you type is uploaded.

Loading the calculator…

Steps to use JSON to Kotlin Data Class

Paste a JSON object or an array of objects and name the root class. Copy or download Models.kt.

How JSON to Kotlin Data Class processes your input

Integers map to Long, decimals to Double, lists to List<T> and varying values to JsonElement. Kotlin keywords are escaped with backticks.

JSON to Kotlin Data Class: worked example

user_name becomes @SerialName("user_name") val userName: String; a coupon present in only one order becomes String? = null.

Generate Kotlin data classes from JSON

  1. Paste a JSON object, or an array of objects, from an API response or a sample file.
  2. Name the root class.
  3. Copy the code or download Models.kt, ready for kotlinx.serialization.

Example: the key user_name becomes @SerialName("user_name") val userName: String, and a coupon present in only one order becomes String? = null.

How types are mapped

  • Integers map to Long, decimals to Double and lists to List<T>. Values whose type varies become JsonElement.
  • Keys that are not camelCase keep their original name through @SerialName, so the JSON still deserializes.
  • Fields missing from some items, or null in the sample, become nullable with a default of null. Kotlin keywords are escaped.
  • Types come from the sample only, so include complete examples. Everything runs in your browser. For iOS, see JSON to Swift; for Java, JSON to Java records.

Questions about JSON to Kotlin Data Class

Does it work with Gson or Moshi?

The annotations target kotlinx.serialization. With other libraries, replace @SerialName with their own name annotation.

Why is a field typed JsonElement?

Because the sample contains different types for that field, so a single Kotlin type would not fit.

Is my JSON uploaded?

No. The classes are generated in your browser.

Is JSON to Kotlin Data Class free, and do I need an account?

Yes. This tool is free to use in your browser without an account. Processing takes place on your device.

What are the limits and what should I check?

Types are inferred from the sample only. Everything is calculated in your browser; nothing you type is uploaded. Review the result before using it in your work.

Is my input sent to a server?

Your input is processed in your browser and is not uploaded by this tool. Files and pasted text are cleared when the page is refreshed. Normal website requests are still required to load the page and its libraries. Read the privacy explanation.