Skip to main content
CalcChef

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. How it works

Use the SQL to JSON

Up to 2,000,000 characters; the SQL is read, never run, and nothing is uploaded.

JSON Output
3 records parsed across 4 columns
Reverse: Convert JSON back to SQL

What is read

  • Values in quotes stay text ('123' and '01234' are strings). Only unquoted numbers, true, false and NULL get a type.
  • Names can be bare, quoted or schema-qualified (public.users). Several INSERT statements are read with their own column lists.
  • Not supported, and reported by name: function calls such as NOW(), DEFAULT, sub-selects, INSERT ... SELECT, X'..' literals, casts. A row with the wrong number of values is an error, not skipped.
  • Query Result reads Markdown, psql, MySQL box and tab-separated tables; cells are text except canonical numbers, true/false and NULL.

About the SQL to JSON

INSERT INTO products (id, name, price, in_stock) VALUES (1, 'Widget', 9.99, true), followed by two more rows, becomes a JSON array of 3 objects, the first {"id": 1, "name": "Widget", "price": 9.99, "in_stock": true}. The converter reads INSERT statements, or a result table pasted from psql, the MySQL client or Markdown. The SQL is read, never run.

SQL values and the JSON they become

SQL valueJSON value
123123
'123'"123"
'01234'"01234"
9.9909.99
1234567890123456789012345678901234567890
truetrue
'true'"true"
NULLnull
'NULL'"NULL"
''""
'It''s'"It's"
N'café'"café"

Only unquoted values get a type. An unquoted number keeps every digit, even past what a JavaScript number can hold.

SQL the converter refuses, and its message

SQLMessage
INSERT INTO t (a, b) VALUES (1);Statement 1 (INSERT INTO t), row 1 has 1 value but the column list names 2.
INSERT INTO t (a) VALUES (NOW());Statement 1 (INSERT INTO t), row 1, value 1: NOW(...) is a function call, which cannot be evaluated here. Replace it with a literal value.
INSERT INTO t (a) VALUES (DEFAULT);Statement 1 (INSERT INTO t), row 1, value 1: DEFAULT is not a literal value (only quoted text, numbers, true, false and NULL are supported). Replace it with a literal.
INSERT INTO t (a) SELECT 1;Statement 1 (INSERT INTO t): INSERT ... SELECT is not supported; only INSERT ... VALUES.

Frequently asked questions

Why does '01234' stay a string in the JSON?
Only unquoted values get a type. '123', '01234', 'true' and 'NULL' in quotes are strings, so a ZIP code keeps its leading zero, while unquoted 123, true and NULL become 123, true and null.
Can I convert a query result instead of INSERT statements?
Switch to Query Result and paste the table from psql, the MySQL client with its +---+ borders, a Markdown table or tab-separated text. Cells are text except canonical numbers, true/false and NULL, so a psql column holding 01234 and 98765 gives "01234" and 98765.
What SQL is not supported?
Anything that needs a database to work out: function calls such as NOW(), DEFAULT, sub-selects, INSERT ... SELECT, X'..' literals and casts such as ::int. Each is reported with its statement, row and value, never guessed. Other statements, such as CREATE TABLE, are skipped and counted.
What if my INSERT has no column list?
The keys become column1, column2 and so on, with a note: INSERT INTO t VALUES (1, 'x') gives [{"column1":1,"column2":"x"}].
Does it read MySQL dumps with backslash escapes?
Turn on “Backslash is an escape”. Then 'it\'s' reads as it's. With it off, as standard SQL reads strings, that string is never closed and the error says to turn the option on. MySQL # comments are skipped either way.
What happens with INSERT statements into several tables?
The output is an object with one array per table: INSERT INTO a (x) VALUES (1); INSERT INTO b (y) VALUES (2); gives {"a":[{"x":1}],"b":[{"y":2}]}.

Related Tools

View all tools