What is a JSON to code converter?
A JSON to code converter reads a sample JSON document and writes the type definitions that describe it. Instead of typing a class by hand for every API response, you paste the response, pick a language and copy the result. It is the quick way to get the shape of a payload into your editor before you write the code that uses it.
This tool covers nine typed targets: TypeScript, Go, Java, C#, PHP, Python (dataclass or pydantic), Rust with serde, Kotlin and Swift. It also writes four value formats: a JSON Schema, a JavaScript object, a PHP array and TOON, a compact text format made for sending data to language models. Each language has a few styles, for example a TypeScript interface or type, a Java record or a class with getters, Go with pointers or plain values for optional fields.
Types are inferred from all the values, not only the first one. If a field is a number in one array item and missing in another, it becomes optional. The generated code was checked against real toolchains: the Go, Java, C#, Rust and Python outputs were compiled and executed, while Kotlin, Swift and PHP were checked with a linter or a parser. Treat the result as a very good first draft, not as a replacement for reading your API documentation. A sample can never tell you that a string field is really an enum.
Everything runs in your browser. The JSON you paste is never uploaded.
How to turn JSON into types
- Paste your JSON in the left box, or load the example order. If the text is not valid JSON, the tool points to the line and column.
- Choose the language. The code on the right changes at once.
- Pick a style if the language offers one, such as
interfaceortype aliasfor TypeScript. - Set the root name. The outermost object gets this name, for example
Order. Nested objects are named after their key. - Choose the indent: 2 spaces, 4 spaces or a tab.
- Turn on "all fields optional" when your sample is only one of many possible shapes.
- Copy the code or download it as a file with the right extension.
Examples
TypeScript and Go from the same payload
Input:
{"id":7,"name":"Ana","tags":["a","b"],"address":{"city":"Lisboa","zip":null},"price":9.5}With root name Order, TypeScript gives:
export interface Order {
id: number;
name: string;
tags: string[];
address: Address;
price: number;
}
export interface Address {
city: string;
zip: null;
}and Go gives:
type Order struct {
ID int `json:"id"`
Name string `json:"name"`
Tags []string `json:"tags"`
Address Address `json:"address"`
Price float64 `json:"price"`
}
type Address struct {
City string `json:"city"`
Zip any `json:"zip"`
}A field that is always null has no known type, so Go falls back to any and Rust to serde_json::Value.
Pydantic and Rust
The same input in Python pydantic and in Rust with serde:
from __future__ import annotations
from pydantic import BaseModel
from typing import Any
class Address(BaseModel):
city: str
zip: Any
class Order(BaseModel):
id: int
name: str
tags: list[str]
address: Address
price: floatuse serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize)]
pub struct Order {
pub id: i32,
pub name: String,
pub tags: Vec<String>,
pub address: Address,
pub price: f64,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Address {
pub city: String,
pub zip: serde_json::Value,
}Optional fields from several items
When the root is a list, the items are merged into one type:
[{"a":1,"b":null},{"a":2}]export type RootList = Root[];
export interface Root {
a: number;
b?: null;
}The field b is missing in the second item, so it is marked optional.
TOON and a number that does not fit
For the first payload above, TOON is:
id: 7
name: Ana
tags[2]: a,b
address:
city: Lisboa
zip: null
price: 9.5But {"id":12345678901234567890} is refused with the message that the number would be rounded by a double. This is on purpose: silent rounding of an identifier is worse than an error. Put the value in quotes in the JSON if it is really an identifier.
Common mistakes
- Trusting one sample. A field that is
nullin your example gets no real type. Use a sample where it is filled, or merge several items in a list. - Whole numbers that are really decimals. If a price is
10in your sample but9.5in another response, the field becomes an integer. Put both kinds in the sample, as two items of a list, and it becomes a decimal. A value written as10.0is already read as a decimal. - Large identifiers. An integer past 2^53 is not safe as a JavaScript number. The JavaScript object output writes it as a BigInt literal, and TOON refuses it.
- Keys that are not valid names.
user-nameor1st placecannot be a field name in most languages. Go getsUserNameandF1stPlacewith the original key kept in thejsontag, and TypeScript quotes the key. Read the result before you rely on it. - Expecting validation. Types describe a shape, they do not check values. For rules such as "at least one item", write a schema and test it with the JSON Schema validator.
- Pasting something that is almost JSON. Comments, single quotes and trailing commas make the input invalid. Clean it first with JSON prettify, or read a JavaScript object with JS object to JSON.
Frequently asked questions
Does it send my JSON to a server?
No. The conversion runs in your browser, so you can paste real responses, although you should still remove secrets before sharing the output.
Why is a field marked optional?
Because it is missing from at least one item in a list, or you turned on "all fields optional". Types are inferred from every value, not only the first.
Can I get a JSON Schema instead of a type?
Yes. Choose "JSON Schema" to get a draft 2020-12 schema with required filled from your sample. To compare two versions of a document, use JSON diff.
How accurate is the generated code?
The Go, Java, C#, Rust and Python outputs were compiled and run against sample documents. Kotlin, Swift and PHP were checked with a lint or parse step only.