JSON to code

Turn a JSON document into types for TypeScript, Go, Java, C#, PHP, Python, Rust, Kotlin or Swift, or into a JSON Schema, a JavaScript object, a PHP

loading JSON to code
About this tool

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

  1. 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.
  2. Choose the language. The code on the right changes at once.
  3. Pick a style if the language offers one, such as interface or type alias for TypeScript.
  4. Set the root name. The outermost object gets this name, for example Order. Nested objects are named after their key.
  5. Choose the indent: 2 spaces, 4 spaces or a tab.
  6. Turn on "all fields optional" when your sample is only one of many possible shapes.
  7. 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: float
use 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.5

But {"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 null in 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 10 in your sample but 9.5 in 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 as 10.0 is 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-name or 1st place cannot be a field name in most languages. Go gets UserName and F1stPlace with the original key kept in the json tag, 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.

Report a problem