Turn a JSON sample into TypeScript interfaces, Go structs and more
This JSON to TypeScript converter reads a sample of your data and writes the matching types: TypeScript interfaces or type aliases, Python dataclasses or TypedDicts, Go structs with json tags, Java classes or records, Kotlin data classes, C# classes, Rust serde structs and Dart models with fromJson and toJson. It is the quick way to get a typed shape for an API response, a config file or a webhook payload without typing every field by hand.
The converter does more than copy keys. It merges every object it finds at the same place, so an array of orders whose second element lacks a coupon gives you an optional field; null values become nullable types; whole numbers and decimals are told apart; and keys such as first-name, class or 1st get safe, idiomatic names with the original key kept in an annotation.
Everything runs in your browser. The JSON never leaves the page, so you can safely paste real responses.
How to use the JSON to Code Types
- Paste a JSON sample into JSON sample, press Load file, or use Sample to see a tricky example.
- Choose a Language. Pick a Style where there is a choice (interface or type alias, dataclass or TypedDict, class or record) and name the root type.
- Tick or clear the options:
export,readonlyandunknownfor TypeScript, serialization annotations orfromJson/toJsonfor the other languages, and idiomatic field names. - Read the generated code on the right. It updates as you type.
- Press Copy code or Download to save a file with the right extension.
JSON to Code Types features
Eight languages
TypeScript, Python, Go, Java, Kotlin, C#, Rust and Dart, each following that language’s naming and null conventions.
Samples are merged
Objects at the same position are combined: fields missing from some objects become optional, and a field that is sometimes null becomes nullable.
Mixed values handled
Arrays of mixed types become union types in TypeScript and Python ((number | string)[]) and the language’s “any” elsewhere. Integers and decimals together become a floating-point type.
Awkward keys survive
Reserved words, keys with spaces or dashes, keys starting with a digit and duplicates after case conversion are renamed, with the original key kept in a tag or comment.
Multiple documents
Paste several JSON documents, one after another, and they are treated as an array of samples and merged.
Precise errors
Invalid JSON is reported with the line and column of the problem.
How values map to types
The table shows the mapping for the most common types. Whole numbers above 2,147,483,647 use a 64-bit integer where the language has one. A JSON value that is only ever null, or an array that is always empty, cannot reveal its type, so it becomes the language’s “any” type (any or unknown in TypeScript, interface{}-style any in Go, serde_json::Value in Rust, dynamic in Dart).
| JSON | TypeScript | Go | Python | Rust |
|---|---|---|---|---|
"text" | string | string | str | String |
42 | number | int | int | i64 |
4.5 | number | float64 | float | f64 |
true | boolean | bool | bool | bool |
null in a field | T | null | *T | T | None | Option<T> |
| field missing in some objects | name?: T | *T with omitempty | = None default | Option<T> |
| array | T[] | []T | list[T] | Vec<T> |
Tips for better types
- Use a representative sample. One record cannot show which fields are optional; include several records, or paste a few documents one after another.
- Rename the root type to something meaningful. Nested types are named after their keys, with plural keys singularised (
ordersgivesOrder). - The generated code is a starting point. For strict contracts, check it against the API’s own schema, for example with the JSON Schema Generator.
- Dates are plain strings in JSON, so they are typed as strings; convert them where you parse the data.
- Format the input first with the JSON Formatter if the parser reports an error, or build realistic samples with the Mock Data Generator.
What the options change
Idiomatic field names converts keys to the convention of the target language: camelCase in Java, Kotlin and Dart, PascalCase in C# and Go (with initialisms such as ID and URL), snake_case in Python and Rust. When a name changes, the original key is kept in a @JsonProperty, @SerialName, [JsonPropertyName], json:"…" or #[serde(rename)] annotation, so the type still matches your data. Turn the option off to keep keys as written where the language allows it.
JSON to Code Types FAQ
How do I convert JSON to a TypeScript interface?
Paste the JSON, leave the language on TypeScript and copy the result. Nested objects become their own interfaces, arrays become T[], and fields missing from some objects become optional with ?.
Can it generate a Dart model from JSON?
Yes. Choose Dart and keep “Add fromJson / toJson” ticked to get a class with a constructor, a factory fromJson and a toJson method, with nullable fields marked ?.
Is my JSON uploaded anywhere?
No. The JSON is parsed and the code is generated by JavaScript in your browser. Nothing is sent to a server.
Why is a field typed as any or dynamic?
The sample only contained null or an empty array for it, so there was nothing to infer from. Add a record where the field has a value.
What happens when an array holds different kinds of objects?
The objects are merged into one type. Fields that appear in every object are required; the rest are optional. Fields with different value types become a union (TypeScript, Python) or the language’s any type.
Does it support JSON with comments or trailing commas?
No. Input must be strict JSON as defined by RFC 8259. The error message names the line and column, so you can fix it quickly.