All tools
Free · Private · No upload

Convert JSON to SQL Online

Turn a JSON array of objects into SQL INSERT statements, with a CREATE TABLE built from the keys. The JSON is parsed in your browser and never uploaded.

Drop a CSV or Excel file here

CSV · XLSX · XLS · XLSM · XLSB

or paste your data below

How to convert JSON to SQL

Three steps from customers.json to a .sql script.

1. Paste the JSON

Paste JSON or drop a .json file. Arrays, keyed objects and JSON Lines all work.

2. Check it in the grid

Check the columns made from nested values: they are inserted as JSON text into a TEXT column. Drop them, or change their type in the CREATE TABLE.

3. Set dialect and table

Pick the dialect, type a table name, tick CREATE TABLE or DROP TABLE IF EXISTS, then copy or download data.sql.

Example: JSON in, SQL out

Customer records with a boolean, a nested address object, a null and an apostrophe in a name.

customers.json
[
  { "id": 1, "name": "Ana Lee", "vip": true, "address": { "city": "Lisbon" } },
  { "id": 2, "name": "Tom O'Hara", "vip": false, "phone": null }
]
data.sql (MySQL, table "customers")
CREATE TABLE `customers` (
  `id` INT,
  `name` TEXT,
  `vip` TEXT,
  `address` TEXT,
  `phone` TEXT
);

INSERT INTO `customers` (`id`, `name`, `vip`, `address`, `phone`) VALUES
(1, 'Ana Lee', 'true', '{"city":"Lisbon"}', NULL),
(2, 'Tom O''Hara', 'false', NULL, NULL);

vip ends up as TEXT holding 'true' and 'false', and the nested address as the JSON text '{"city":"Lisbon"}'. The null phone is inserted as NULL. For real booleans, change vip to BOOLEAN in PostgreSQL; MySQL needs 1 and 0.

What changes between JSON and SQL

Column types are guessed

In this example id is INT, name, vip, address and phone are TEXT. The rule: whole numbers give INT, a decimal point gives DOUBLE, anything else TEXT, dates included (other dialects use their own names, such as INTEGER and REAL).

Column names

Headers are turned into safe identifiers (characters other than letters, digits and _ become _; a leading digit gets a col_ prefix; duplicates get _2, _3). id, name, vip, address and phone are already valid, so they are kept as they are.

Values

The first row is inserted as (1, 'Ana Lee', 'true', '{"city":"Lisbon"}', NULL): numbers bare, text in single quotes, with ' doubled as in 'Tom O''Hara'. The empty phone for 1 becomes NULL.

Columns from keys

Columns are the union of all keys, in the order they first appear. A key missing from an object leaves that cell empty: phone for 1.

When to convert JSON to SQL

JSON to SQL in JavaScript

JSON needs no parser: JSON.parse gives you the data. Then one INSERT is built, with the same quoting rules as this page.

import { readFile } from 'node:fs/promises';

const items = JSON.parse(await readFile('customers.json', 'utf8'));
const toText = (v) => (v == null ? '' : typeof v === 'object' ? JSON.stringify(v) : String(v));
const header = [...new Set(items.flatMap((o) => Object.keys(o)))];
const rows = items.map((o) => header.map((key) => toText(o[key])));

const id = (name) => '`' + String(name).trim().replace(/[^A-Za-z0-9_]/g, '_') + '`';
const value = (v) => {
  if (v == null || String(v).trim() === '') return 'NULL';
  return isNaN(v) ? `'${String(v).replace(/'/g, "''")}'` : String(v).trim();
};
console.log(`INSERT INTO ${id('customers')} (${header.map(id).join(', ')}) VALUES\n` +
  rows.map((row) => `(${header.map((_, i) => value(row[i])).join(', ')})`).join(',\n') + ';');

TabularJS reads spreadsheet files, not JSON; JSON.parse is all JSON needs.

npm install tabularjs MIT licensed

Frequently asked questions

Can nested data go into a PostgreSQL JSONB column?

Yes, with one manual edit: change the column's type from TEXT to JSONB in the CREATE TABLE. The inserted values are valid JSON text, so PostgreSQL accepts them.

What happens to keys that aren't valid column names?

They are cleaned up: characters other than letters, digits and _ become _, and a key starting with a digit gets a col_ prefix, so first-name becomes first_name.

What happens to nested objects and arrays?

Each one is kept in a single cell as JSON text. To get separate columns (address.city, address.zip), flatten the objects in code first, or split the column in the grid before converting to SQL.

On SQL Server, should I keep the TEXT columns?

Better not: TEXT is deprecated in SQL Server. Change those columns to NVARCHAR(MAX) or a sized NVARCHAR in the CREATE TABLE before running it.

Does customers.json get uploaded when I convert it to SQL?

No. The JSON is parsed in your browser and never uploaded.