SQL Schema to JSON

A .sql dump describes your schema perfectly well, but it describes it as text. If you want to generate TypeScript interfaces, build a data dictionary, or diff two schema versions programmatically, you first need that structure as data. This parses CREATE TABLE statements into JSON.

How to use it

  1. Drop in a .sql file containing one or more CREATE TABLE statements.
  2. Parsing runs in the page, so proprietary schemas never leave your machine.
  3. Download the JSON description.

What gets extracted

Each CREATE TABLE statement becomes an object with the table name and an ordered list of columns. Every column records its declared name, its type including any length or precision arguments, and the constraints attached to it, such as NOT NULL, PRIMARY KEY, UNIQUE, and DEFAULT values.

Column order is preserved, because it is meaningful. It determines the physical layout a migration will produce and it affects the shape of any INSERT that omits an explicit column list.

Dialect differences you will run into

SQL is standardised in theory and divergent in practice. Identifier quoting alone differs three ways: MySQL uses backticks, PostgreSQL and the standard use double quotes, and SQL Server uses square brackets. All three are recognised here.

Type names diverge further. SERIAL in PostgreSQL, AUTO_INCREMENT in MySQL, and IDENTITY in SQL Server all express the same intent with entirely different syntax. Types are captured as written rather than normalised to a common vocabulary, because normalising would require choosing a target dialect and would discard information that matters when the point of the exercise is comparing dialects.

What this is not

This is a schema parser, not a SQL engine. It reads structure, not data. INSERT statements, stored procedures, triggers, and views are skipped rather than parsed, since each is a substantially different grammar and none of them describe table shape.

It is also not a validator. A CREATE TABLE that references a type your database does not have will parse here quite happily. Confirming that the schema is actually deployable is the database server job.

At a glance

Accepted input.sql
Statements parsedCREATE TABLE
Identifier quotingBackticks, double quotes, square brackets
Column orderPreserved

Frequently asked questions

Does it parse INSERT statements or table data?

No. It reads schema structure only. A dump containing both will yield the table and column definitions, and the data statements are skipped.

Which SQL dialect should my file be in?

MySQL, PostgreSQL, and SQL Server CREATE TABLE syntax all parse, including their different identifier quoting styles. Type names are recorded exactly as written rather than translated.

Are foreign keys captured?

Inline column-level REFERENCES clauses are captured with the column. Table-level constraint blocks declared separately at the end of the statement are recorded as constraints on the table.

Can I use the output to generate TypeScript types?

That is one of the main reasons to produce it. The JSON gives you table names, column names, declared types, and nullability, which is everything a code generator needs as input.

Read more

CSV, JSON, YAML, XML: choosing a data format — Every conversion between these formats loses something. Knowing what, in advance, prevents most of the resulting bugs.

Related tools