Xerobit

SQL to JSON Converter

Paste SQL INSERT statements to convert them into a JSON array of objects. This sql to json converter extracts column names from each statement and maps each row of values accordingly. It runs entirely in your browser, so nothing is uploaded to any server.

Paste one or more INSERT statements. Column names are taken from the first statement.

JSON output

[
  {
    "id": 1,
    "name": "Alice",
    "active": true
  }
]

How to use the sql to json converter

  1. Paste your INSERT statements into the SQL input area.
  2. Set the indent size for the output formatting (use 0 for compact JSON).
  3. The JSON array appears instantly as you type or paste.
  4. Copy the result for use in your application or API.

How the sql to json conversion works

The converter uses a hand-written tokenizer that scans your SQL text character by character. It recognises single-quoted strings (including doubled quotes like ''), double-quoted identifiers, backtick-quoted identifiers, numeric literals, keywords such as NULL, TRUE and FALSE, line comments starting with -- and block comments between /* and */.

Once tokenized, the parser looks for the pattern INSERT INTO ... (columns) VALUES (row1), (row2). Each statement uses its own column list, and every value in a row is paired with its column name. For example, given INSERT INTO t (id, name) VALUES (1, 'Alice'), the sql to json output is [{"id": 1, "name": "Alice"}].

Supported syntax and limits

This tool handles the most common form of INSERT statements but does not attempt to be a full SQL dialect parser. It supports:

  • Standard string literals with single quotes and escaped quotes via doubling.
  • Integer and decimal numbers, including negative values.
  • The keywords NULL, TRUE and FALSE (case-insensitive).
  • Double-quoted and backtick-quoted column or table identifiers.
  • Line comments (--) and block comments (/* */).

Bare words, unquoted text and double-quoted values are rejected with an error. It does not support expressions inside VALUES (such as 1 + 2 or function calls), subqueries, hex literals, binary strings, or PostgreSQL dollar-quoted strings. If your SQL uses these features, simplify the values before converting.

Practical use cases

Converting SQL data to JSON is useful when migrating records from a relational database into a document store or an API response format. Developers often export test fixtures as INSERT scripts and need them as JSON arrays for integration tests written in JavaScript or Python.

If you need to go the other direction and turn tabular data into SQL, the csv to sql insert tool can generate those statements for you. For minifying existing SQL scripts, see sql minify, and for exporting query results to a flat format, try sql to csv.

Handling special characters

SQL strings use single quotes, and a literal single quote inside a string is represented by two consecutive single quotes. The tokenizer correctly unescapes these sequences. For instance, the SQL value 'O''Brien' becomes the JSON string "O'Brien".

Numeric values are parsed as JavaScript numbers. Number text that is valid JSON is copied exactly as written, so long integers keep every digit in the output (a JavaScript program that reads the JSON may still round them). Hex values such as 0x1F, leading zeros and a leading plus sign are converted to plain numbers. String values preserve all Unicode characters exactly as they appear in the SQL input.

Reference: SQL types to JSON types

SQL valueJSON outputNotes
4242Integer number
3.143.14Decimal number
'hello'"hello"String literal
'it''s'"it's"Escaped single quote
NULLnullNull keyword
TRUEtrueBoolean keyword
FALSEfalseBoolean keyword

Worked examples

ExampleInputResult
Single rowSQL input: INSERT INTO users (id, name) VALUES (1, 'Alice');; Indent spaces: 2[ / { / "id": 1, / "name": "Alice" / } / ]
Multiple rowsSQL input: INSERT INTO products (sku, price, in_stock) VALUES ('A-1', 9.99, TRUE), ('B-2', NULL, FALSE);; Indent spaces: 2[ / { / "sku": "A-1", / "price": 9.99, / "in_stock": true / }, / { / "sku": "B-2", / "price": null, / "in_stock": false / } / ]
Escaped quotesSQL input: INSERT INTO notes (id, text) VALUES (1, 'She said ''hello''');; Indent spaces: 2[ / { / "id": 1, / "text": "She said 'hello'" / } / ]

FAQ

Does this convert SELECT queries to JSON?

No. The sql to json converter only processes INSERT statements because they contain explicit column names and value lists that map cleanly to JSON objects. A SELECT query lacks inline data to convert.

How are NULL values handled?

The SQL keyword NULL (case-insensitive) is converted to the JSON literal null. This preserves the distinction between a missing value and an empty string.

Can I convert multiple INSERT statements at once?

Yes. Paste several statements separated by semicolons. Each statement keeps its own column list, and all rows are appended to the same JSON array in the order they appear.

Are my database credentials or data sent anywhere?

No. The entire conversion happens locally in your browser using JavaScript. Nothing is uploaded to any server.

What if my SQL contains expressions like NOW() or 1+1?

Expressions inside VALUES are not supported. The parser expects simple literals: strings, numbers, NULL, TRUE or FALSE. Replace expressions with their evaluated results before converting.

How do I handle backslash-escaped strings from MySQL?

This tool follows the standard SQL convention where single quotes are escaped by doubling them. Backslash escapes are treated as literal characters. Adjust your input to use doubled quotes if needed.