How to Convert SQL INSERT Statements to JSON
A seed file, a database dump, or a snippet from a ticket — sometimes the data you need is stuck inside INSERT statements and you need it as JSON.
SQL INSERT statements are a perfectly good way to store data and an awkward one to read from application code. When you need the same rows as JSON — to seed a test fixture, feed a front-end mock, or compare against an API response — the conversion is simple in principle: each statement becomes an object, each column name becomes a key. The edge cases are where it goes wrong.
The basic mapping
One INSERT statement becomes one JSON object:
INSERT INTO users (id, name, active, deleted_at, note) VALUES (1, 'Ravi', TRUE, NULL, 'O''Brien''s friend'); INSERT INTO users (id, name, active, deleted_at, note) VALUES (2, 'Priya', FALSE, NULL, '42');
becomes a JSON array with one object per row:
[
{
"id": 1,
"name": "Ravi",
"active": true,
"deleted_at": null,
"note": "O'Brien's friend"
},
{
"id": 2,
"name": "Priya",
"active": false,
"deleted_at": null,
"note": "42"
}
]That is exactly what the SQL to JSON tool produces for these statements. The rest of this guide is about the cases where the mapping isn't obvious.
How values map to JSON types
SQL literals don't carry a type the way JSON values do, so a converter has to infer one from how the value is written:
- Bare numbers (
42,3.14) become JSON numbers. - Quoted values are always strings — including
'42'. The quotes are the signal, so a number stored as text stays text. Look at the last row in the example above:"note": "42". TRUEandFALSEbecome JSONtrueandfalse.NULLbecomes JSONnull, not the string"NULL"— an easy thing to get wrong in a hand-rolled script.
Things that trip people up
Multi-row INSERTs. Dumps from tools such as mysqldump often pack many rows into one statement: INSERT INTO t (a, b) VALUES (1, 'x'), (2, 'y');. Each parenthesised tuple is its own row, so that statement should produce two objects, not one. A simple hand-rolled converter that stops at the first closing parenthesis will silently return only the first row, so whichever tool you use, check that the number of objects in the JSON matches the number of rows you expect. The SQL to JSON tool reads every tuple in a multi-row statement.
Column names. JSON keys come from the column names, so INSERT INTO users VALUES (1, 'Ravi') — with no column list — gives the converter nothing to name the keys with. A default mysqldump file omits column lists but includes the CREATE TABLE, and the SQL to JSON tool takes the names from it when both are pasted together. Without a matching CREATE TABLE it names the keys col_1, col_2… and says so in a warning; add the column list if you want real names.
Escaped quotes. Standard SQL escapes a single quote by doubling it, so 'O''Brien' is the string O'Brien. That is handled. A backslash escape like 'O\'Brien' is accepted by MySQL by default (unless NO_BACKSLASH_ESCAPES is on), but in standard SQL and PostgreSQL a backslash is an ordinary character. The tool decodes backslash escapes when it is reading the SQL as MySQL, which auto-detect chooses only when it sees MySQL-specific syntax such as backticks, ENGINE= or AUTO_INCREMENTwritten as SQL (not inside a string value), and it then warns that it decoded escapes. Otherwise such a statement is usually reported as an error rather than guessed at, so select MySQL for MySQL dumps.
Values that are not plain literals. A function call such as now() or an expression such as 1 + 2 is not evaluated. It comes out as a JSON string holding the SQL text, and the tool warns you; replace those values yourself if you need real data.
Dates and timestamps. A value such as '2026-01-15 10:30:00' is a quoted literal, so it stays a string in the JSON. JSON has no date type, so that is correct — but whatever reads the JSON needs to parse it.
What the tool reads, and what it skips. It reads INSERT ... VALUES (and REPLACE,INSERT IGNORE and SQLite's INSERT OR REPLACE), single- and multi-row, with quoted names in any of the four dialects. Strings may use doubled quotes, MySQL backslash escapes, PostgreSQL E'...' and dollar quoting, SQL Server N'...' and MySQL character-set prefixes. Comments follow the dialect: block comments nest in PostgreSQL and SQL Server, and in MySQL -- must be followed by a space. It skips, and counts in a warning, INSERT ... SELECT, DEFAULT VALUES, the SET form and PostgreSQL COPY ... FROM stdin data blocks. It skips the bodies of triggers, procedures and functions (including MySQL DELIMITER blocks) and mentions how many other statements were ignored in a note. Hex and bit literals such as 0x1F or B'1' come out as text (in SQLite and PostgreSQL 16+ 0x1F is the integer 31). With no other sign of the dialect, 0x... is kept as text. Anything else is reported rather than guessed at.
Check the result before you rely on it
- Count the objects in the output against the rows you expected.
- Telling the body of a trigger, procedure or function apart from the data around it relies on reading
BEGIN ... ENDblocks, andbeginandendcan also be column names. When the tool skips a routine this way it adds a warning to the result (a routine under aDELIMITERline, which has an exact end, needs none). It is still a heuristic: in an unusual routine (for example a body that is a singleIForWHILEstatement with noBEGIN, in a script without aDELIMITERline, or a column or label literally namedbeginorendin an unusual position) it can read a statement from inside the routine as data, or skip anINSERTafter it, and in some of those cases it gives no warning. If the row count looks wrong, split the routines out of the dump and convert the data on its own. - Numbers with more than 15 digits are written exactly, but JavaScript's
JSON.parserounds them; the tool warns, and a big-number-aware reader (or keeping them as text) avoids it. - Spot-check a row with a
NULL, a quoted number, and an apostrophe — those are the three that break first. - Remember that SQL
NULLand an empty string''are different values; they come out asnulland""respectively.
Going the other way
If you need the reverse — JSON to INSERT statements — see importing JSON API responses into a SQL database. For a spreadsheet-friendly format instead of JSON, the SQL to CSV tool takes the same input.
Try it on your own statements
Paste your INSERT statements into SQL to JSON to get a JSON array instantly. It runs entirely in your browser, so nothing in your data is uploaded.
Try it yourself — free, runs entirely in your browser.
Open SQL to JSON