JSON to Rust
Runs in your browser; nothing is uploaded
Paste a JSON sample and get types for it in TypeScript, Go, Python, Java, C#, Kotlin or Rust. Every object in an array is read, so optional and nullable fields come out right. Nothing is sent.
JSON
Types
Nothing yet
How JSON becomes types
A JSON response says what its data looks like, but not in a form your compiler can check. Writing the matching types by hand is slow and easy to get subtly wrong: a field that is sometimes missing, a number that is sometimes a decimal, a value that is sometimes null.
This tool reads the whole sample, not just the first object. When an array holds several objects, every one is read: a key missing from any of them becomes optional, a key that is null in any of them becomes nullable, and a number that is a whole number in one and a decimal in another becomes a decimal type. Nested objects become their own named types, named after their key, and array items get the singular (orders holds Order).
The types use each language's usual JSON library, so they work as they are: TypeScript interfaces, Go structs with json tags, Pydantic models for Python, Java records with Jackson, C# classes for System.Text.Json, Kotlin data classes for kotlinx.serialization and Rust structs for serde. A key that isn't a valid name in the language, such as created-at, keeps its JSON name through the library's rename attribute.
How to use this tool
- 1Paste a JSON sample, such as an API response, or pick an example.
- 2Choose the language, and name the top-level type (Root by default).
- 3Read the types on the right: they update as you type. Copy them, or download them as a file.
- 4For the most accurate types, paste a response with several items in its arrays, and include the cases where fields are missing or null.
Troubleshooting
- A field is typed as any, unknown or Object
- The sample had different kinds of value for it (for example a string in one item and a number in another), or it was only ever null. Paste a sample where the field has a real value, or change the type by hand.
- A field that is always present is marked optional
- It was missing from at least one object in an array. If that object was a special case, remove it from the sample.
- This can't be read
- The input isn't valid JSON. The status bar shows the line and column; the JSON formatter explains common mistakes such as trailing commas and single quotes.
Frequently asked questions
Is my JSON uploaded?
No. The types are worked out in your browser; nothing you paste leaves it.
Why are integers int64 or long?
JSON numbers have no size, and IDs and timestamps often outgrow 32 bits. Change them to a smaller type if you know the range.
Can it read a JSON Schema or an OpenAPI spec instead?
This tool reads example data. For a spec, open it in the API Map, which shows every schema it defines.