Saved

Press the star next to a tool's name to save it.

All my tools

JSON to Types Converter

Paste sample JSON and this JSON to types converter writes TypeScript interfaces, Go structs with json tags, or Python TypedDict and dataclass types. It merges array items and marks optional keys.

Use a sample with several items so optional keys can be spotted. Numbers like 1.0 are read as whole numbers.

Everything is worked out in your browser. Nothing you type is sent anywhere.

Result

Paste JSON and press Convert to see the types.

A JSON to types converter reads a sample of JSON and writes the type definitions that describe it, so you do not have to type them by hand. Paste a response from an API, choose TypeScript, Go or Python, and press Convert. The JSON is analysed in your browser and is never uploaded.

How the JSON to types converter infers types

JSON has four simple kinds of value (string, number, boolean and null) and two structured kinds, objects and arrays. [1] The converter walks your sample and records which kinds it sees in every position. Numbers are split into whole numbers and numbers with a fraction, which is useful for Go and Python even though the JSON format itself has a single number type. [1] Because this is guessing from an example, the result is only as complete as the sample you give it.

Merging array items

Arrays in JSON may hold values of different kinds. [1] When an array contains several objects, the tool merges their shapes into one type. A key that appears in every item becomes a required field. A key missing from some items becomes optional. If one item has a string and another has null, the field becomes a nullable string. If the kinds really differ, such as a number in one item and text in another, the field becomes a union, or the loosest type the language offers. This merge is our own working, designed to give a safe type for the whole array.

TypeScript output

Each object becomes an exported interface. An optional property is written with a question mark after its name, which lets the property be left out. [2] Arrays use the T[] form, and unions are written with a vertical bar and wrapped in brackets when they sit inside an array. Nullable values add | null. An empty array has no items to learn from, so its element type is unknown. When the whole sample is an array or a single value, the converter writes a type alias for the root and an interface for the item.

Go output

Go decodes JSON into struct fields that must be exported, which means the name starts with a capital letter, and a tag tells the decoder which JSON key belongs to which field. [3] The converter turns keys such as user_id into UserID and profile_url into ProfileURL, using a short list of common initialisms. A field that may be missing or null becomes a pointer with the omitempty option; the Go documentation says omitempty drops a nil pointer or zero value when encoding. [3] Mixed kinds and empty arrays use the any type. When Go decodes into an empty interface, a number becomes float64, which is why mixed-number fields stay as any rather than a guessed int. [3]

Python output

Python gives two choices. The first is a TypedDict class for each object; keys that are not valid names, such as my-key, use the call form of TypedDict. Missing keys are wrapped in NotRequired, which arrived in Python 3.11. [4] The second is a dataclass with snake_case field names, where optional fields default to None and are placed last so the class is valid. Nullable values use Optional, and mixed kinds use Union. [4] Nested classes are written before the classes that use them.

Worked example

Take {"id": 1, "name": "Ada", "tags": ["a", "b"], "address": {"city": "London"}, "note": null}. In TypeScript you get an interface Root with id: number, name: string, tags: string[], address: Address and note: null, plus an interface Address with city: string. In Go you get a Root struct with ID int, Name string, Tags []string, Address Address and Note any, each with its tag, and an Address struct. In Python you get a Root class and an Address class.

JSON sampleTypeScriptGoPython
1numberintint
1.5numberfloat64float
["a"]string[][]stringlist[str]
nullnullanyNone
missing in some itemskey?: T*T with omitemptyNotRequired[T]

Check the result before you rely on it

  • Use a sample with several items and with every optional key present at least once.
  • A value such as 1.0 is read as a whole number, because JavaScript does not keep the trailing zero. Change that field to a decimal type by hand if needed.
  • Whole numbers beyond 9,007,199,254,740,991 lose precision in any JavaScript-based parser, so use strings for very large IDs. The JSON specification itself warns that only integers in that range are reliably shared between programs. [1]
  • Duplicate names in an object are possible in the text, but the standard says names should be unique, so the tool keeps the last one. [1]
  • Date-like text stays a string. Add a date type yourself.

When two nested objects would get the same type name, the second gets a number, such as Item2, so the output always compiles. Rename types freely afterwards; the tool is a fast first draft, not a schema. Since everything happens in your browser, you can paste real API responses without sharing them.

What next

Use this often? Add JSON to Type to your Home screen.

Frequently asked questions

What does a JSON to types converter do?

It reads sample JSON and writes type definitions for it in TypeScript, Go or Python, using nested types for objects and lists for arrays.

How are optional keys found?

When several objects appear in one array, a key missing from some of them is marked optional. A key present in all of them is required.

What happens with null values?

A value that is null in some items and a string or number in others becomes a nullable type. A value that is always null becomes null, or any in Go.

Can it handle a list at the top level?

Yes. The tool writes a type alias for the list and a type for the items, named after the main type name you choose.

Is my JSON uploaded anywhere?

No. The JSON is read and analysed in your browser only, so API responses with private data stay on your device.

Are the types exact?

They describe the sample you pasted. Check them against the real API documentation, since a sample may lack keys or values that appear in other responses.

What went wrong?

We only receive the reason you pick, never what you typed into the tool.

Thanks for your feedback.

More Developer Tools