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.
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.
| Column | Type | NULLs |
|---|---|---|
| id | 0 | |
| name | 0 | |
| active | 0 | |
| address_city | 0 |
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 column | PostgreSQL | MySQL | SQLite | SQL Server |
|---|---|---|---|---|
| true and false | BOOLEAN | TINYINT(1) | INTEGER | BIT |
| Whole numbers up to 2,147,483,647 | INTEGER | INT | INTEGER | INT |
| A whole number above that, such as 3000000000 | BIGINT | BIGINT | INTEGER | BIGINT |
| A whole number of about 9.22 × 10¹⁸ or more, such as 1e20 | NUMERIC | DECIMAL(65,0) | NUMERIC | FLOAT |
| Decimals such as 9.99 | DOUBLE PRECISION | DOUBLE | REAL | FLOAT |
| Text | TEXT | TEXT | TEXT | NVARCHAR(MAX) |
| Mixed kinds, such as numbers and text | TEXT | TEXT | TEXT | NVARCHAR(MAX) |
| Objects stored as JSON text, and arrays | JSONB | JSON | TEXT | NVARCHAR(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
| Dialect | The text O'Brien \ x is written | Skip 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 Server | N'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 toolsCSV to JSON
Convert CSV to JSON: the first row becomes the keys, quoted commas and line breaks are kept, and numbers and booleans are detected only if you ask.
JSON to CSV
Turn JSON into CSV: an array of objects, one object or a list of values. Flatten nested objects, expand arrays into rows, then copy or download.
XML to JSON Converter
Convert XML to JSON and JSON back to XML: attributes go under @attributes, repeated elements become arrays, and an XML error shows its line and column.
YAML to JSON Converter
Convert YAML to JSON with anchors and merge keys resolved, YAML 1.2 or 1.1 typing for yes/no, and a warning for numbers JSON cannot hold.
JSON to YAML
Convert JSON to YAML in block or flow style. Text such as yes or 017 is quoted so it stays a string, and a JSON error shows its line and column.
SQL to JSON
Turn SQL INSERT statements, or a result table pasted from psql, MySQL or Markdown, into JSON. Quoted values stay strings; numbers, true and NULL get types.