A JSON to SQL converter turns JSON data into SQL you can run against a database. Paste an array of objects, choose MySQL, PostgreSQL, SQLite or SQL Server, and this JSON to SQL converter writes a CREATE TABLE statement with a column for every key and INSERT statements for every row, with values quoted safely. Copy the SQL or download it as a .sql file.
INSERT statements
An SQL INSERT statement adds one or more records to a table in a relational database. Its basic form is INSERT INTO table (column1, column2) VALUES (value1, value2), and the number of columns and values must be the same. Since SQL-92, a single INSERT can add several rows at once with multiple sets of values, a feature supported by SQL Server, PostgreSQL, MySQL, SQLite and others. The converter uses this multi-row form, grouping up to 100 rows in each statement, which is much faster to run than one statement per row.
How the JSON is mapped
- Each object becomes one row.
- Each key becomes a column; keys missing from some objects get NULL in those rows.
- Numbers stay numbers; whole numbers get an integer column and decimals a floating-point column.
- true and false become the database's boolean values.
- Text is wrapped in single quotes, with any single quote inside doubled, so it can't break the statement.
- null becomes NULL.
- Nested objects and arrays are stored as JSON text; MySQL and PostgreSQL get a JSON column type.
Database differences
Each database quotes names and names types a little differently, so pick the one you'll run the SQL on. MySQL quotes table and column names with backticks; PostgreSQL and SQLite use double quotes; SQL Server uses square brackets. Quoting names means columns called order or group, which are reserved words, still work. Column types are chosen to suit each database, such as BIGINT, DOUBLE PRECISION and JSONB for PostgreSQL, or INTEGER, REAL and TEXT for SQLite.
How to use the JSON to SQL converter
- Paste your JSON: an array of objects, or a single object.
- Enter a table name, such as users.
- Choose your database.
- Untick Include CREATE TABLE if the table already exists.
- Press Convert to SQL, then copy or download the result.
Example
This JSON:
[{"id": 1, "name": "Ava", "active": true}, {"id": 2, "name": "Ben", "active": false}]
becomes, for MySQL, a CREATE TABLE for users with id BIGINT, name TEXT and active BOOLEAN, then: INSERT INTO `users` (`id`, `name`, `active`) VALUES (1, 'Ava', 1), (2, 'Ben', 0);
Before you run it
The generated CREATE TABLE is a starting point. Review the column types and add what the converter can't know: a primary key, NOT NULL constraints, indexes and sensible text lengths. Values must satisfy the table's constraints; if one doesn't, the database returns an error and the row isn't added. Try the SQL on a test database first, and back up real data before importing into it.
Is it private?
Yes. The conversion happens in your browser. Your JSON isn't uploaded, and the SQL isn't run anywhere; it's only text until you run it yourself.
Column types explained
The converter looks at every value in a column to choose its type. If all values are whole numbers, the column is an integer; if any have decimals, it's a floating-point number; if they're all true or false, it's a boolean; if they mix numbers and text, the column falls back to text so every value fits. Objects and arrays go into a JSON column where the database has one, or text where it doesn't. A column that's empty in every row becomes text.
Large datasets
For big files, the INSERT statements are split into groups of up to 100 rows each. That keeps each statement a manageable size for database tools and import limits while still being much faster than inserting one row at a time. If your database has a smaller limit on statement size, run the statements in batches or use its bulk import feature with a CSV instead.
Importing the SQL
Run the downloaded .sql file with your database's command-line client or a graphical tool. For MySQL and MariaDB, use the mysql client or a tool such as phpMyAdmin; for PostgreSQL, psql or pgAdmin; for SQLite, the sqlite3 shell; for SQL Server, SQL Server Management Studio. Run it against a test database first and check a few rows before importing into a live one.
Reserved words and odd names
JSON keys don't follow database naming rules: they can contain spaces, start with numbers or match SQL keywords such as order, group or select. Quoting every table and column name keeps them working in the generated SQL. If you'd rather avoid quoted names in your queries, rename the keys in the JSON first to simple lower-case names with underscores.
Common mistakes
- Choosing the wrong database, so quoting doesn't match.
- Running CREATE TABLE when the table already exists.
- Relying on guessed types without checking them.
- Invalid JSON, such as single quotes or trailing commas.
What next
Use this often? Add JSON to SQL to your Home screen.
Frequently asked questions
How does the JSON to SQL converter work?
It reads your JSON, makes a column for each key with a suitable type, and writes CREATE TABLE and multi-row INSERT statements for your database.
Which databases are supported?
MySQL and MariaDB, PostgreSQL, SQLite and SQL Server; each gets its own name quoting and column types.
Are text values escaped?
Yes. Text is wrapped in single quotes and any single quote inside is doubled, so it can't break the statement.
What happens to nested JSON?
Nested objects and arrays are stored as JSON text, in a JSON column where the database supports one.
Is my data uploaded or run?
No. It's converted to text in your browser and nothing is run.
Sources
Was this tool helpful?
What went wrong?
We only receive the reason you pick, never what you typed into the tool.
Thanks for your feedback.