JSON to SQL

Generate CREATE TABLE and INSERT statements from a JSON array.

The JSON to SQL converter turns a JSON array of objects into an optional CREATE TABLE statement plus INSERT statements for MySQL, PostgreSQL or SQLite. Column types are inferred from the values and strings are escaped for the chosen dialect. It is handy for seeding a database from an API export or a fixture file.

Turning JSON records into SQL

A JSON array of objects is a list of records, which maps naturally onto a table: each object is a row and each key is a column. To load it into a relational database you need a CREATE TABLE statement with a type for every column and INSERT statements with correctly quoted values. The tool collects every key found in any object as a column, in order of first appearance, and fills missing keys with NULL. Types are inferred by looking at all values in a column: whole numbers become integers, decimals become floating-point, true and false become booleans, strings become text, and nested objects or arrays are stored as JSON text. Each database writes these types and booleans differently, so the Dialect option changes the type names, identifier quoting and boolean literals.

How to use JSON to SQL

  1. Paste JSON Paste an array of objects (or a single object) into the JSON (array of objects) box.
  2. Set the table Enter a Table name and pick a Dialect: MySQL, PostgreSQL or SQLite.
  3. Tune the output Set Rows per INSERT and tick or untick Include CREATE TABLE.
  4. Copy the SQL Copy the result from the SQL box, or use Download to save output.sql, and run it in your database client.

Frequently asked questions

How are nested objects and arrays stored?

As JSON text in a single column: type JSON in MySQL, JSONB in PostgreSQL and TEXT in SQLite. The value is written as a quoted JSON string.

How are missing keys handled?

The set of columns is the union of keys across all objects. Where an object has no value for a column, NULL is inserted.

Are identifiers and strings escaped?

Yes. Table and column names are quoted (backticks for MySQL, double quotes for PostgreSQL and SQLite), and string values have single quotes doubled. MySQL strings also have backslashes escaped.

Why is a column text when most values are numbers?

If a column mixes numbers with strings or booleans, the tool cannot pick a numeric type and uses a text type. Clean the data so each key has one type.

Is the generated SQL safe to run on production?

Review it first. The tool builds statements from your data without knowing your schema, and it does not add keys, constraints or transactions.

Is my JSON sent to a server?

No. The SQL is generated in the page and the input is not sent anywhere.