JSON to SQL: the complete guide
How types map, how identifiers and strings must be quoted, and how to run the result safely.
Why convert JSON to SQL?
JSON is the default format for everything that talks over HTTP. SQL is the default format for everything that stores relations. Almost every system you work with has to move data between the two — and the conversion is where data quietly gets corrupted.
The usual sources are:
- API responses into a local database — for exploration, for a demo, or for a fixture file.
- Webhook payloads into a queue table — so you can inspect what a partner actually sent.
- Legacy exports into a new system — the old system exports JSON, the new system wants rows.
- Third-party datasets — a vendor sends a JSON file, your warehouse wants a bulk load.
How do JSON types map to SQL types?
The mapping is mostly obvious, and mostly where imports break.
| JSON | SQL literal | Notes |
|---|---|---|
string | 'value' | Always quoted; internal quotes doubled |
number | 42 | Emitted unquoted so numeric columns accept it |
true / false | TRUE / FALSE | Standard SQL keywords |
null | NULL | Not the string 'null' |
object | '{...}' | Serialised as JSON text |
array | '[...]' | Serialised as JSON text |
| missing key | NULL | When records have different keys |
The single most common bug in hand-written SQL is rendering a JSON null as the quoted string 'null'. Your column then contains the four characters null instead of SQL NULL, and every WHERE x IS NULL query silently returns nothing.
How are identifiers quoted?
SQL identifiers — table names, column names, schema names — are quoted differently depending on the database:
- PostgreSQL, SQLite, SQL Server, Oracle, ANSI SQL: double quotes,
"users". This is the SQL standard and the default in HopConvert. - MySQL and MariaDB: backticks,
`users`. MySQL also accepts double quotes by default, but only inANSI_QUOTESmode — switching to MySQL quoting avoids ambiguity. - No quoting: also offered, for databases and migration tools that prefer bare identifiers.
Quoting matters for two reasons. First, reserved words — order, group, user, select — are common as column names, and an unquoted reserved word is a syntax error. Second, mixed-case identifiers are folded to lower case by some databases but not others, so quoting is what keeps your column names exactly as you defined them.
How are string literals escaped?
A string literal in SQL is wrapped in single quotes, and any single quote inside it is written twice:
'O''Brien'
That is the ANSI rule and it is what HopConvert emits for PostgreSQL and SQLite. MySQL additionally interprets backslash sequences inside string literals, so a literal backslash must be doubled and control characters must be escaped:
'C:\\path\\to\\file'
'line one\nline two'
HopConvert handles that automatically when the MySQL dialect is selected. Getting this wrong is the classic cause of "syntax error at or near" errors in the middle of an import.
How is the column list decided?
The converter reads every record and builds the column list from the union of all keys, in order of first appearance. Records that lack a key get NULL for that column. This matches the way you would write the CREATE TABLE statement yourself, and it is what makes a sparse JSON array importable without hand-editing.
Nested objects are flattened into column names joined by __ — {"user": {"name": "Alice"}} produces the column user__name. Match that to your schema, or rename the column in the database.
Should I use one INSERT per row or a multi-row INSERT?
Both are valid, and the difference is large in practice.
- One INSERT per row: easy to read, easy to resume, and each statement is independent. But every statement is a round trip, so ten thousand rows means ten thousand round trips.
- A single multi-row INSERT: one statement, one round trip, dramatically faster for bulk loads. Postgres, MySQL, SQLite, and SQL Server all accept it.
For a few dozen rows the difference is irrelevant. For a bulk load it is the difference between seconds and minutes. The multi-row form is the default in HopConvert for that reason.
Very large batches can also hit the maximum statement size in Postgres, so for really big payloads, generate chunks rather than one enormous statement.
Common edge cases
- Top-level object instead of an array: treated as a single row.
- Top-level array of scalars: converted to a single column named
value. - Nested arrays and objects: serialised to JSON text and stored as a text column.
- Missing keys: written as
NULL. - Numbers beyond JavaScript's safe integer range: JSON.parse loses precision on integers above 2^53-1. For 64-bit IDs, keep them as strings in the source JSON.
- Very long strings: fine, subject to your database's column limit.
- Unicode and emoji: written as UTF-8.
Running the generated SQL safely
A few habits make a JSON-to-SQL import far less painful:
- Always wrap the whole thing in a transaction —
BEGIN;before,COMMIT;after. If one row fails, nothing is half-applied. - Test against a copy first. Run it on a snapshot, not production.
- Check the column list against your schema before running. The converter derives columns from the JSON, not from your table.
- Use
ON CONFLICT DO NOTHING(orINSERT IGNORE) if re-running a backfill. - Mind the transaction log. A million-row insert in one transaction can be heavy; chunk it.
And one more thing worth remembering: the generated SQL is generated from data you control, but it is still code. Read it before you run it against anything you care about.
Frequently asked questions
Everything you need to know about converting JSON into SQL INSERT statements.
What is a JSON to SQL converter?
How do I convert JSON to SQL?
Is HopConvert free to use?
Is my JSON uploaded anywhere?
Which SQL dialects are supported?
How are nulls and booleans handled?
Should I use one INSERT per row or a multi-row INSERT?
What about nested objects in the JSON?
__, so {"user": {"name": "Alice"}} becomes the column user__name. Nested arrays and objects are serialised as JSON text in a single column.Try the JSON to SQL converter
Free, instant, and private — your JSON never leaves this tab.
More free tools: JSON to CSV · CSV to JSON · XML to CSV · JSON to YAML · YAML to JSON · JSONL to CSV · JSON to SQL · JSON to Markdown · JSON to Excel