araç köşesi

JSON to Classes and Structs in 7 Languages

Turn sample JSON into a Python dataclass, C# record, Go struct, Kotlin data class, Rust serde struct, Swift Codable or Dart class

Your data stays with you. Conversion happens inside the browser; nothing is sent to a server.

How it works

To work with an API response as typed objects, you first need types that match its JSON, and translating a twenty-field nested response by hand is slow and full of typos. Paste a sample response into the left pane, pick a language from the list and type a root type name such as Order; the code appears on the right as you type. In the sample, the "shipping" object becomes its own type (Shipping) and the "items" array becomes a list of Item, the singular of the array name. All entries of an array are merged into one type: the second item has no discount, so that field becomes float | None in Python, double? in C#, *float64 in Go and Option<f64> in Rust. Whether a number is whole or decimal is read from how it is written, so 59.0 stays a decimal. A key like "customer_name" is renamed to fit the language (CustomerName in Go, customerName in Kotlin) and the original name is kept in a tag or annotation so the JSON library still matches it. The comment at the top of the output shows the one line that reads your JSON in that language.

This tool is also known as json to python, json to python class, json to c#, json to go struct, json to kotlin data class, json to rust struct, json to swift codable, json to dart.

What is Data class (record, struct)?

A data class is a type written to carry data rather than behavior: its fields and their types are declared, and the compiler or a library fills in the rest. Python's @dataclass, C#'s record, Kotlin's data class and the structs of Go, Rust and Swift all do this job. Mapping a JSON response onto such types catches misspelled field names and type mix-ups while you write code instead of at runtime.

What is Serialization?

Serialization turns an object in your program into portable text such as JSON; deserialization builds the object back from that text. Every language has a library for it: System.Text.Json, encoding/json, serde, Codable, kotlinx.serialization. These libraries match field names to JSON keys, and when the names differ, the tags and annotations this page adds tell them which key belongs to which field.

What is the difference between JSON Schema and language types?

Language types (dataclass, struct, record) are for reading JSON inside one program: the compiler or runtime checks field names and types, but only in that language. JSON Schema is a language-neutral contract; it validates whether a JSON document is acceptable with the same rules in every language and is used in API documentation. If your own code reads the data, use the types from this page; if you are writing rules that several teams and languages must follow, JSON Schema fits better.

What each language gets, and which library it needs

Each language follows its community's usual approach, so the code drops into a project without rework. Python uses @dataclass from the standard library plus a from_dict class method that also builds nested objects, because a dataclass cannot build itself from a dict. C# output is a positional record for the built-in System.Text.Json. In Go, struct fields start with a capital letter so they are exported, and the json:"..." tag carries the original key; abbreviations such as id, url and api follow Go style as ID, URL and API.

Kotlin output needs the kotlinx.serialization plugin; Rust output needs the serde and serde_json crates (serde with the derive feature in Cargo.toml). Swift needs nothing beyond Codable. Dart output is a hand-written fromJson/toJson pair that works in Flutter without extra packages or a code generator such as json_serializable.

  • Python 3.7+: @dataclass + from_dict, an optional field is float | None = None
  • C# 9+: public record Order(...) + [property: JsonPropertyName("orderId")]
  • Go 1.18+: *float64 + json:"discount,omitempty"
  • Kotlin: @Serializable data class, @SerialName("customer_name")
  • Rust: #[derive(Serialize, Deserialize)], Option<f64>, #[serde(rename)]
  • Swift 5: struct Order: Codable + enum CodingKeys
  • Dart 3 / Flutter: final fields, factory fromJson, toJson

Pitfalls: one sample, nulls and mixed types

Types are inferred only from the sample you paste. A field that is never null in the sample but arrives as null in production will make your code fail at that moment, so paste an array with several records when you can: once records are merged, fields that are missing or null in some of them become optional on their own. A field that is only ever null has no known type and falls back to a type that holds any JSON value (Any in Python, JsonElement in C#, serde_json::Value in Rust, a JSONValue enum added to the Swift output).

If a field is a number in one record and text in another, the same catch-all type is used; an empty array's element type is unknown too. Date strings (2026-09-27) and small value sets are not turned into date types or enums; they stay strings, and narrowing them is up to you. Reserved words such as class, type or self cannot be field names: Python gets class_, Kotlin and Swift use backticks, Rust uses r#type, and the original JSON name is kept in the tag.

Frequently asked questions

How do I convert JSON to a Python class?

Paste sample JSON into the left pane, pick Python and type the root type name. The right pane shows a @dataclass for every nested object and a from_dict method that builds the object from a JSON dict. Read your data with json.loads and pass it to Order.from_dict(...).

Why do Go struct fields start with a capital letter?

In Go only fields that start with a capital letter are exported, and encoding/json can only fill exported fields. So the customer_name key becomes a CustomerName field, and the json:"customer_name" tag makes sure it is matched to the right key when decoding. Abbreviations such as id and url are written ID and URL, following Go style.

Why are some fields Optional, ? or Option?

That field is missing from some records of the array, or it was null in the JSON. If the language does not know a value can be empty, decoding fails. If you are sure every field is always present, set Fields missing from some records to Required; fields that were actually null stay optional.

Is the JSON I paste sent to a server?

No. Type inference and code generation run entirely in your browser, so even a sample response with customer records or access keys never leaves this page. Once loaded, the page also works offline.