JSON to SQL seed scripts in one step
Fixtures, API exports and test data often exist as JSON, while the database wants SQL. This JSON to SQL converter reads an array of records and writes a CREATE TABLE statement with a column for every field, followed by INSERT statements with properly quoted and escaped values. Paste the result into your SQL client, a migration or a seed file.
Example
[
{ "id": 1, "name": "O'Brien", "score": 9.5, "active": true, "joined": "2024-03-01", "plan": null },
{ "id": 2, "name": "Kim", "score": 7, "active": false, "joined": "2023-12-24" }
]
With table name users and MySQL quoting:
CREATE TABLE `users` (
`id` INT,
`name` VARCHAR(255),
`score` DECIMAL(18,6),
`active` BOOLEAN,
`joined` DATE,
`plan` VARCHAR(255)
);
INSERT INTO `users` (`id`, `name`, `score`, `active`, `joined`, `plan`) VALUES (1, 'O''Brien', 9.5, TRUE, '2024-03-01', NULL);
INSERT INTO `users` (`id`, `name`, `score`, `active`, `joined`, `plan`) VALUES (2, 'Kim', 7, FALSE, '2023-12-24', NULL);
The apostrophe in O'Brien was doubled, which is the standard SQL way to escape a single quote. The explicit null became NULL, and so did the second record's plan, although that record has no plan key at all: a missing key is written as NULL. Only an empty string in the JSON, "", is written as ''.
How column types are inferred
Each column is typed by looking at all its non-empty values:
| Values in the column | Type |
|---|---|
| Whole numbers up to 2,147,483,647 | INT |
| Whole numbers, some larger | BIGINT |
| Numbers with any fraction | DECIMAL(18,6) |
true / false only | BOOLEAN |
Strings like 2024-03-01 | DATE |
Strings like 2024-03-05T10:22:00Z | DATETIME (MySQL), TIMESTAMP (ANSI), DATETIME2 (SQL Server) |
| Other strings, mixed types or no values | VARCHAR(255), or TEXT when a value is longer than 255 characters |
The types are a starting point. Add primary keys, NOT NULL constraints and tighter lengths before using the table in production.
Options
- Table name – used in both statements;
my_tableif left empty. - Identifier quoting – wraps table and column names in backticks (MySQL, MariaDB), double quotes (PostgreSQL, SQLite and ANSI SQL) or square brackets (SQL Server). None leaves simple names bare but still double-quotes names with spaces, dots or symbols. Bare names can collide with reserved words such as
orderoruser, so quoting is the safer default. - Include CREATE TABLE – untick it when the table already exists and you only need rows.
- Single multi-row INSERT – writes one
INSERT … VALUES (…), (…);statement instead of one per record. It is shorter and usually loads faster; for very large data sets, split it into chunks your database accepts. - Flatten nested objects – nested fields become columns named with dots, such as
meta.plan. With it off, nested objects and arrays are inserted as JSON text, for example'{"plan":"pro"}', which you can store in a JSON or JSONB column after changing the type.
Dialect notes
- SQL Server has no
BOOLEANtype and noTRUE/FALSEliterals. Change the column toBITand the values to1and0. - PostgreSQL does not accept backticks. Choose the ANSI option so identifiers use double quotes.
- Dotted column names from flattening are legal when quoted, but awkward to type in queries. Rename them to
meta_planin the JSON or the output if you prefer. - MySQL and backslashes. Only single quotes are escaped. MySQL treats a backslash in a string as an escape character by default, so a value such as a Windows path can change meaning. Double the backslashes or enable the
NO_BACKSLASH_ESCAPESSQL mode before loading such data.
Before you run the script
- Check numeric and date columns for empty strings. A missing key becomes
NULL, but a value of""is written as'', and PostgreSQL and MySQL in strict mode reject that row for anINTorDATEcolumn. Change those values tonullin the JSON. - Look at the
VARCHAR(255)columns. A column that mixes numbers and text falls back to a string type, which may hint at inconsistent data in the source. - Run the statements inside a transaction or against a scratch database first, so a failed row does not leave the table half filled.
Tidy long scripts with the SQL formatter. If your data is a spreadsheet export, CSV to SQL does the same job, and SQL to JSON turns INSERT statements back into records.
Privacy
The statements are generated in your browser; your JSON is not uploaded and nothing is executed against any database. Load URL and Save & Share are the only features that reach our server.