JSON to Kotlin data class
Paste JSON and get Kotlin data classes for it below, as you type, ready for kotlinx.serialization: @Serializable, with @SerialName wherever a key isn't its property's name.
Nullable and optional
A value that is null somewhere is nullable (String?), and a key some items lack is nullable with null as its default, so the class reads data without it. Lists are List<T>, and objects keyed by ids or dates Map<String, T>.
Values of any kind
kotlinx.serialization reads no Any, so values of mixed kinds, and integers past 64 bits, are JsonElement (from kotlinx.serialization.json), to read further as you need them.
Types from sample data
Every item of a list, and every value of JSON Lines, is a sample of the same type: a key that some lack is optional, a value that is sometimes null is nullable, whole numbers mixed with decimals are decimals, and objects with the same keys share one type, named after the key they are under ("line_items" holds LineItem). An object whose keys are ids or dates, such as {"1001": {…}, "1002": {…}}, is a map. YAML, TOML, CSV and the other formats work too: pick their language above the editor.
Or the data as mapOf
Data writes the JSON with mapOf and listOf, typed Map<String, Any?>, with $ escaped in strings: test data for Android or the JVM.
Copy, download or switch
The Kotlin follows your typing, and a mistake in the JSON is pointed out first, with its line and column. Copy or Download (a .kt file) take it as it is. Above the code, switch between Types and Data, or pick another language; the Convert menu's Code item opens the one you picked last.
Private: nothing is uploaded
Formatting, checking and comparing all happen in your browser. Your documents are saved in the browser's own storage on this computer, not on a server, so they never leave your device.
With your permission, we count anonymous visits (which pages and tools are used), never what you type. See Privacy.
Questions
How do I read the JSON?
Json.decodeFromString<Root>(text). For JSON with keys the classes leave out, use Json { ignoreUnknownKeys = true }.
Do the classes work with Gson or Moshi?
Yes, as data classes: swap @SerialName for Gson's @SerializedName or Moshi's @Json(name = …), and drop @Serializable.
Is my data uploaded anywhere?
Your documents aren't: everything runs in your browser, and they are saved on your computer, in the browser's storage for this site. With your permission we count anonymous visits, never their content.
More tools
- Formatter & Converter for JSON, YAML, XML, SQL
- JSON to TypeScript – Interfaces from JSON
- JSON to Go Struct Converter
- JSON to C# Class Converter
- JSON to Java Class & Record Converter
- JSON to Python Dataclass, Pydantic & Dict
- JSON to Dart Model with fromJson & toJson
- JSON to Swift Codable Struct Converter
- JSON to Rust Struct Converter (serde)
- Free Online Diagram Maker & Editor
- Crypto Tools: Passwords, Keys, Hashes & JWTs
- Timestamp, Cron, Regex & UUID Utilities