Browser-based SQL tool · your JSON is read here, never sent

JSON to SQL Table Schema

Paste JSON or JSON Lines samples and get CREATE TABLE statements for PostgreSQL, MySQL, SQLite or SQL Server, with INSERTs. Nested objects and arrays turn into child tables with foreign keys, flattened columns or JSON columns, as you choose.

Samples

Drop a .json or .jsonl file

Up to 6 MB

Options

JSON to SQL CREATE TABLE and INSERT Generator

Operator: Abdulla Al Fahad. How tools are built and checked

Three customer records is not much JSON, yet it already holds an address object, a list of tags and a list of orders, and a relational database wants none of that inside one cell. Pasted here, those three records become four tables: records, records_address, records_tags and records_orders, linked by foreign keys, with ten INSERT rows between them. Every type comes from the values actually present: a field that is always a whole number below 2,147,483,647 becomes INTEGER, a timestamp with a Z suffix becomes TIMESTAMPTZ, and a string field that reaches 50 characters is sized VARCHAR(50).

Here the records can differ from one another, keys can be missing, and structure can nest several levels deep. The output is a first draft to review and edit, not a finished design: it knows what your sample contains, not what your application will send next year.

Key features

  • PostgreSQL, MySQL, SQLite and SQL Server output, each with its own types, identity syntax and quoting
  • JSON arrays, a single object or JSON Lines (one object per line), up to 20,000 records and 5,000,000 characters
  • Nested objects as child tables, flattened columns (address_city) or JSON columns; arrays as child tables or JSON columns
  • Natural keys found automatically (id, _id, uuid, guid, table_id, key) when unique and never null, otherwise a generated id column
  • INSERT statements for none, 100, 1,000 or all rows per table, batched so no statement grows too large
  • A column report with the inferred type, nullability, how many values were seen and a note where the data was mixed, downloadable as CSV

How types, keys and nested data are decided

Each value is classified before the column type is chosen: null, boolean, integer, number with a fraction, UUID, a calendar date (YYYY-MM-DD, checked to exist), a timestamp, or other text. A column whose values are all integers is INTEGER, BIGINT when any value passes 2,147,483,647, or NUMERIC(38,0) when any value passes 2^53 (9,007,199,254,740,992), where a JSON double can no longer hold every integer; that widened column gets a note in the column report. Integers mixed with fractions become DOUBLE PRECISION (DOUBLE, REAL or FLOAT in the other dialects). Dates mixed with timestamps become a timestamp. Anything else that mixes kinds, for example numbers and strings in one field, is stored as text and the column report says so. Text is sized to the next of 50, 100, 255, 500, 1,000 or 4,000 characters above the longest value, and longer than that becomes TEXT (NVARCHAR(MAX) on SQL Server); the TEXT-only option skips sizing.

A column is NOT NULL only when it was present and non-null in every record, and the option can be switched off if your sample is too small to trust. Timestamps keep their offset: a column with any Z or +02:00 value is TIMESTAMPTZ on PostgreSQL and DATETIMEOFFSET on SQL Server. MySQL has no zone-aware type, so its values are converted to UTC and written as DATETIME(3). SQLite has no date type, so dates and timestamps stay TEXT in ISO 8601 form, which sorts correctly.

Child tables are named parent_field, receive a foreign key named parent_key with ON DELETE CASCADE, and are created after their parent. An object that appears once per parent adds a UNIQUE constraint to that key, so the one-to-one shape is enforced. An array of plain values becomes a table with a single value column. Empty arrays produce no rows. Arrays that mix objects with plain values, objects nested deeper than six levels, and anything beyond 60 tables or 400 columns fall back to a JSON column or are reported. If the document is a single object whose every value is a non-empty array of objects, such as {"users": [...], "orders": [...]}, each key becomes its own table instead of one row with children.

How it works

Records are walked once to collect rows and queue nested values; keys are chosen, then child tables are built with the parent's key already known so each foreign key can be filled in. Identifiers are lower-cased snake_case with accents removed and quoted only when they are reserved words or contain unusual characters.

The INSERT block writes explicit values for generated ids so child rows can refer to them: PostgreSQL gets a setval() line afterwards so the identity counter continues after the last id, and SQL Server wraps the rows in SET IDENTITY_INSERT ON and OFF. MySQL foreign keys are written as table-level constraints, because MySQL accepts but ignores an inline REFERENCES clause.

Common use cases

  • An API returns orders with a customer object and a line_items array, and a 200-record sample is pasted to get PostgreSQL tables orders, orders_customer and orders_line_items before any code is written.
  • A MongoDB export in JSON Lines is loaded into SQLite for analysis: the generated tables are created, the INSERT block is run, and the one field that mixed numbers and strings is noticed in the column report.
  • A log of 300 webhook deliveries in JSON Lines is loaded into SQLite to count event types: every delivery has id, type and created, while the data object differs by type, so data is switched to a JSON column and the rest becomes a clean events table.
  • A configuration file with a settings object that has no predictable keys is switched to JSON columns for nested objects, giving one table with a JSONB column instead of twenty one-row tables.

How to use this tool

  1. Paste the JSON or JSON Lines, or drop a .json or .jsonl file, then press Generate SQL.
  2. Pick the database and decide how nested objects and arrays should be stored; the output regenerates as each option changes.
  3. Read the column report and the warnings, then change keys, sizes and any column marked as mixed.
  4. Copy the SQL or download the .sql file, and run it on an empty database first.

Example

Three records (excerpt)

[{"id":1,"name":"Ada Lovelace","email":"ada@example.com","active":true,"score":9.5,"joined":"2024-03-01T10:15:00Z","address":{"city":"London","zip":"N1"},"tags":["math","poetry"],"orders":[{"sku":"A-1","qty":2,"price":10.5},{"sku":"B-2","qty":1,"price":3}]}, ...]

PostgreSQL, first table and the orders child table

CREATE TABLE records (id INTEGER PRIMARY KEY, name VARCHAR(50) NOT NULL, email VARCHAR(50), active BOOLEAN NOT NULL, score DOUBLE PRECISION NOT NULL, joined TIMESTAMPTZ NOT NULL);
CREATE TABLE records_orders (id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, records_id INTEGER NOT NULL, sku VARCHAR(50) NOT NULL, qty INTEGER NOT NULL, price DOUBLE PRECISION NOT NULL, FOREIGN KEY (records_id) REFERENCES records (id) ON DELETE CASCADE);
4 tables, 10 rows: records 3, records_address 2, records_tags 2, records_orders 3. email is nullable because the second record has null.

Edge cases to watch

JSON numbers are read as ordinary doubles, so an integer above 2^53 (9,007,199,254,740,992) may already have been rounded before this tool sees it; the column is widened to NUMERIC(38,0) and the column report says so. Keep such identifiers as strings in the source if they must stay exact.

A string such as "007" or "12345" stays text, because a sample cannot show whether leading zeros matter. Only real JSON numbers become numeric columns.

The generated keys and cascades are conventions, not design. There are no indexes beyond the foreign-key ones on PostgreSQL, SQLite and SQL Server, no CHECK constraints and no enum types. For rows that are already flat, the CSV to SQL converter on this site is the simpler route.

Rows are inserted for the first 100 records of each root table by default, and children are kept only when their parent was kept, so the counts in a sample load can differ from the totals in the file.

  • Include the records with the most keys and the longest strings in the sample: sizing and nullability only reflect what was pasted, so a rare long value is the one that later fails to insert.
  • Switch nested objects to flattened columns when each object has a fixed shape, such as a postal address; keep child tables when the object can repeat or grow.

Common errors and how to fix them

"Line 3 is not valid JSON, so the input is neither one JSON document nor JSON Lines"
The text is not one JSON value and one line in it is not an object either. A trailing comma, a comment or a log prefix on that line is the usual cause; fix the line it names.
"2 of 5 records are not objects"
The array mixes objects with numbers, strings or nulls. Remove the stray values; if every record is a plain value, the tool instead creates one table with a single value column.

Frequently asked questions

Why does a column say it stores mixed value types as text?

The same key held, for example, a number in one record and a string in another. A numeric column would reject the string, so the column is text and every value is inserted as its text form. Clean the source data if the column should be numeric.

Can I get one table instead of child tables?

Yes. Set nested objects to flattened columns or a JSON column, and arrays to a JSON column. The result is a single table, with arrays kept as JSON.