Skip to main content
CalcChef

JSON to SQL

Generate SQL INSERT statements, and CREATE TABLE if you want it, from JSON for PostgreSQL, MySQL, SQLite or SQL Server, with types inferred from every row. How it works

Use the JSON to SQL

Up to 2,000,000 characters. Your JSON and these settings are saved in this browser (local storage) so they are here next visit; nothing is uploaded. Text longer than 100,000 characters is not saved. Use Clear Saved Values to remove what is saved.

SQL Output
Used when Include CREATE TABLE is ticked.
Nested objects
Output

Columns in table users

2 rows, 4 columns, 1 INSERT statement. Types are inferred from every row; pick another type from a column's list to override it.

ColumnTypeNULLs
id0
name0
active0
address_city0

About the JSON to SQL

The users sample on this page becomes INSERT INTO "users" ("id", "name", "active", "address_city") VALUES with the rows (1, 'Ada', TRUE, 'London') and (2, 'Alan', FALSE, 'Wilmslow') for PostgreSQL. The converter writes INSERT statements, and CREATE TABLE when you tick it, for PostgreSQL, MySQL, SQLite or SQL Server, with column types inferred from every row.

Column types inferred in each dialect

JSON values in the columnPostgreSQLMySQLSQLiteSQL Server
true and falseBOOLEANTINYINT(1)INTEGERBIT
Whole numbers up to 2,147,483,647INTEGERINTINTEGERINT
A whole number above that, such as 3000000000BIGINTBIGINTINTEGERBIGINT
A whole number of about 9.22 × 10¹⁸ or more, such as 1e20NUMERICDECIMAL(65,0)NUMERICFLOAT
Decimals such as 9.99DOUBLE PRECISIONDOUBLEREALFLOAT
TextTEXTTEXTTEXTNVARCHAR(MAX)
Mixed kinds, such as numbers and textTEXTTEXTTEXTNVARCHAR(MAX)
Objects stored as JSON text, and arraysJSONBJSONTEXTNVARCHAR(MAX)

Types come from every row; null values are ignored. A type picked in the column table replaces the inferred one. Every column is nullable except a primary key you name, and no other keys or constraints are written. SQL Server FLOAT is the 53-bit floating-point type, so a whole number beyond the BIGINT range keeps about 15 significant digits there.

Quoting and skipping existing rows in each dialect

DialectThe text O'Brien \ x is writtenSkip rows that already exist
PostgreSQL'O''Brien \ x'INSERT INTO ... ON CONFLICT DO NOTHING
MySQL'O''Brien \\ x'INSERT IGNORE INTO
SQLite'O''Brien \ x'INSERT OR IGNORE INTO
SQL ServerN'O''Brien \ x'Not available: the box is turned off

Frequently asked questions

How are SQL column types chosen from JSON?
From every row, not just the first. A column holding 1 in one row and "a" in another is TEXT, and its numbers are written as text. Whole numbers up to 2,147,483,647 are INTEGER (INT in MySQL) and larger ones BIGINT. A column with 1 in one row and 2.5 in another takes the decimal type (DOUBLE PRECISION in PostgreSQL). true in one row and 1 in another is also TEXT, so true is written as 'true'. To use another type, pick it in the column table: values are still written as the JSON holds them, except that a text type such as TEXT or VARCHAR(255) quotes every value, so id 1 is written '1'.
How do I skip rows that already exist?
Tick “Skip rows that already exist”: PostgreSQL gets ON CONFLICT DO NOTHING, MySQL INSERT IGNORE and SQLite INSERT OR IGNORE. They skip only rows that break a unique constraint, such as the primary key column you can name for CREATE TABLE. In MySQL, IGNORE also turns data errors into warnings. SQL Server has no INSERT form that skips rows, so the box is off for it: create a unique index with IGNORE_DUP_KEY = ON, which Microsoft documents as leaving out only the duplicate rows with a warning, or write a MERGE on your own key.
What happens to nested JSON objects?
By default they are flattened: {"address": {"city": "London"}} becomes the column address_city. You can store them as JSON text instead (JSONB in PostgreSQL, JSON in MySQL, TEXT in SQLite) or skip them. Arrays are stored as JSON text unless you skip nested values.
How many rows go in one INSERT statement?
100 by default; you can pick 1, 50, 100, 500 or 1000. At the default, 250 rows give 3 INSERT statements. 1000 is also the most SQL Server accepts in one VALUES list; Microsoft documents error 10738 above it.
Why are backslashes doubled in the MySQL output?
MySQL reads a backslash in a string as an escape by default, so O'Brien \ x is written 'O''Brien \\ x'. If your server runs with NO_BACKSLASH_ESCAPES, change each doubled backslash back to one. PostgreSQL and SQLite keep the backslash as it is.
Is my JSON saved?
Yes, in this browser’s local storage, with the settings, so it is there on your next visit. Text over 100,000 characters is not saved. Clear Saved Values removes what is saved.

Related Tools

View all tools