JSON to TypeScript, Python and SQL

Turn a JSON object, or an array of objects, into a TypeScript interface, a Python class, a Pydantic model or SQL. Pretty-printing stays on the JSON formatter

What this page builds

An API sample is often one JSON object, or a list of objects. This page writes a TypeScript interface, a Python class, a Pydantic model, or a CREATE TABLE and one INSERT per object. Formatting and repair stay on the JSON formatter.

How to use it

  1. Paste JSON into the box. Update as you type is on.
  2. Mode is TypeScript interface, SQL CREATE TABLE + INSERT, Python class or Pydantic model. Name starts as Item. Optional properties starts checked. The last checkbox is export on TypeScript, and imports on Python or Pydantic.
  3. Convert runs once. Show Example fills {"id":1,"name":"Ada","ok":true,"tags":["a","b"],"meta":{"n":2}}, sets Mode to TypeScript interface, Name to Item, and leaves both checkboxes on. The result has export interface Item with id?: number, name?: string, ok?: boolean, tags?: string[] and meta?: ItemMeta, plus export interface ItemMeta with n?: number.
  4. Switch Mode to SQL CREATE TABLE + INSERT. The table is item. The columns are id BIGINT NOT NULL, name TEXT NOT NULL, ok BOOLEAN NOT NULL, tags TEXT NOT NULL and meta TEXT NOT NULL. The INSERT is 1, 'Ada', 1, '["a","b"]', '{"n":2}'.
  5. Switch Mode to Python class and leave Optional properties and imports checked. The result starts with from dataclasses import dataclass and from typing import Optional, List, then @dataclass class ItemMeta with n: Optional[int] = None, then @dataclass class Item with id, name, ok, tags: Optional[List[str]] = None and meta: Optional[ItemMeta] = None. Switch Mode to Pydantic model: the imports add from pydantic import BaseModel and both classes extend BaseModel, with no @dataclass. Show Example stays on TypeScript interface.
  6. Clear All empties both boxes. Copy Result copies the output.

How the types are chosen

The root must be one object, or an array of objects. A number, a string, or an array of numbers stops with an error. An object with no keys stops with an error.

TypeScript uses boolean, number and string. An array of one of those types becomes T[]. An empty array becomes unknown[]. A nested object becomes another interface: the parent name plus the key, so meta under Item is ItemMeta. Mixed types become unknown. Optional properties adds ? on every field. When the root is an array and a key is missing from some objects, that field is optional even if Optional properties is off. export writes export interface. An array root adds the comment // JSON root is Name[].

SQL lowercases Name and turns other characters into underscores, so Order Line becomes order_line. Integers are BIGINT, other numbers are DOUBLE, booleans are BOOLEAN and are written as 1 or 0, and strings are TEXT. A nested object or array is TEXT holding the JSON text. A single quote inside a string is doubled. A key that is null, or missing on any row, is nullable. Each object is one INSERT.

Where people use it

  • Drafting a TypeScript interface from an API sample.
  • Loading a JSON array when nested fields can stay JSON text.
  • Checking that a quote in a name is stored as two quotes.

Questions

Why is meta a second interface?

It is an object, so TypeScript names it ItemMeta. In SQL that same field is TEXT containing {"n":2}.

Why did a list of numbers fail?

Each object becomes a row. A list of numbers has no columns.