JSON to Code

JSON to JSON Schema

Paste a JSON sample. Get JSON Schema types, and the code that reads and writes the JSON. It runs in your browser, and your data never leaves it.

Open the tool with JSON Schema chosen

An example

This JSON:

{
  "id": 42,
  "name": "Ada Lovelace",
  "active": true,
  "joined": "2020-01-02T03:04:05Z",
  "tags": ["engineer", "writer"],
  "address": { "city": "London", "postcode": null },
  "scores": [9.5, 8, 10]
}

becomes this JSON Schema:

{
    "$schema": "http://json-schema.org/draft-06/schema#",
    "$ref": "#/definitions/Root",
    "definitions": {
        "Root": {
            "type": "object",
            "additionalProperties": false,
            "properties": {
                "id": {
                    "type": "integer"
                },
                "name": {
                    "type": "string"
                },
                "active": {
                    "type": "boolean"
                },
                "joined": {
                    "type": "string",
                    "format": "date-time"
                },
                "tags": {
                    "type": "array",
                    "items": {
                        "type": "string"
                    }
                },
                "address": {
                    "$ref": "#/definitions/Address"
                },
                "scores": {
                    "type": "array",
                    "items": {
                        "type": "number"
                    }
                }
            },
            "required": [
                "active",
                "address",
                "id",
                "joined",
                "name",
                "scores",
                "tags"
            ],
            "title": "Root"
        },
        "Address": {
            "type": "object",

and 18 more lines. Open the tool to see the whole file.

With the reader and the writer

The tool also writes the code that turns the text into those types, and back again. That code checks the shape as it reads, which is the part a type alone cannot do.

{
    "$schema": "http://json-schema.org/draft-06/schema#",
    "$ref": "#/definitions/Root",
    "definitions": {
        "Root": {
            "type": "object",
            "additionalProperties": false,
            "properties": {
                "id": {
                    "type": "integer"
                },
                "name": {
                    "type": "string"
                },
                "active": {
                    "type": "boolean"
                },
                "joined": {
                    "type": "string",
                    "format": "date-time"
                },
                "tags": {
                    "type": "array",
                    "items": {
                        "type": "string"
                    }
                },
                "address": {
                    "$ref": "#/definitions/Address"
                },
                "scores": {
                    "type": "array",
                    "items": {
                        "type": "number"
                    }
                }
            },
            "required": [
                "active",
                "address",
                "id",
                "joined",
                "name",
                "scores",
                "tags"

and 23 more lines. Open the tool to see the whole file.

What you need in your project

Nothing. A JSON Schema is itself JSON.

A JSON Schema is the best thing to keep in your repository. Make it from a sample one time, read it, correct it, then make the code for every language from the schema.

A schema holds what a sample cannot: which field is required, what a number may be, what a string must look like, and a description of each field.

The output follows draft 06. Most checkers read it.

The trap to know about

A schema made from one sample marks everything as present. Open it and decide what is truly required before you commit it.

The options you can change

These belong to JSON Schema, so the row of switches changes with the language.

OptionWhat it doesDefault

Questions

How do I turn JSON into JSON Schema?

Paste your JSON into the box, choose JSON Schema, and the code appears at once. Give the top type a name, and every other name follows from your keys. Then copy the code, or download it as a file.

Does my JSON go to a server?

No. The generator runs inside your browser, in a worker. Nothing is uploaded, nothing is stored, and the tool works with the network turned off.

What do I need to add to my JSON Schema project?

Nothing. A JSON Schema is itself JSON.

How does it know which fields are optional?

From your sample. A key that is missing, or holds null, becomes optional. One sample cannot show everything, so give more than one, or start from a JSON Schema.

Other languages

JSON to Zod JSON to Effect Schema JSON to PropTypes All the guides

Try it on your own JSON. The generator runs in your browser. Nothing is uploaded, and nothing is stored on a server.

Open JSON to Code