How to Import JSON into a SQL Database?

APIs and exports give you data as JSON. But sometimes you want to filter, join and count that data. Then a SQL database is often a better place for it. So how do you move the data from JSON into SQL?

This guide shows the whole process. You will turn JSON into rows and create the table. You will also handle nested data, escape values safely and load many rows quickly.

Quick answer: Each object in your JSON array becomes one row, and each key becomes a column. Create a table with matching columns. Then add the rows with INSERT statements. You can turn nested objects into extra columns, or keep them in a JSON column.

How does JSON map to a SQL table?

The easiest JSON to import is an array of objects. Every object has about the same keys:

[
  { "id": 1, "name": "Ada", "email": "ada@example.com", "active": true },
  { "id": 2, "name": "Sam", "email": "sam@example.com", "active": false }
]

Here is how the parts match up:

  • The array becomes a table.
  • Each object becomes a row.
  • Each key becomes a column.

Sometimes your data is one object with the array inside it, like {"users": [...]}. Then import the inner array. If some objects don’t have a key, that column is just NULL (empty) for them.

Step 1: Create the table

A CREATE TABLE statement tells the database the name and type of each column. For the example above:

CREATE TABLE users (
  id INTEGER PRIMARY KEY,
  name TEXT,
  email TEXT,
  active BOOLEAN
);

You can write this by hand. Or the JSON to SQL converter can write it for you. It looks at every row to choose the column types:

  • Whole numbers become INTEGER or BIGINT.
  • Decimal numbers become NUMERIC.
  • True and false become BOOLEAN (BIT on SQL Server).
  • Everything else becomes TEXT.

You pick the SQL dialect (the version of SQL your database uses): PostgreSQL, MySQL, SQLite or SQL Server.

Then check the result. A converter can’t know everything about your data:

  • Add a primary key (the column that gives each row a unique ID). Add NOT NULL where a value must always be there.
  • JSON has no date type, so dates come in as text. Change those columns to DATE or TIMESTAMP if you want to sort and filter by date.
  • Large IDs need BIGINT. In some languages, they can lose digits before they even reach the database. See big numbers in JSON.

Step 2: Make the INSERT statements

Each row becomes an INSERT. Several rows can share one statement:

INSERT INTO users (id, name, email, active) VALUES
  (1, 'Ada', 'ada@example.com', TRUE),
  (2, 'Sam', 'sam@example.com', FALSE);

In the converter, paste your array. Set the table name and the dialect. Then choose if you want the CREATE TABLE statement too.

Copy the SQL, or download it as a .sql file. Then run it with your database program.

Do you want to check the result, or go the other way? The SQL to JSON converter turns INSERT statements, or a dump file, back into a JSON array.

How do you escape quotes safely?

In SQL, text values go inside single quotes. So a name with an apostrophe, like O’Brien, ends the text too early. This breaks the statement.

The standard fix is to write the quote twice:

INSERT INTO users (name) VALUES ('O''Brien');

Watch out for a few more things:

  • Column names. Keys like order or user-id are not plain SQL names. You must put quotes around them. Use "order" in PostgreSQL and SQLite, `order` in MySQL, and [order] in SQL Server.
  • Backslashes in MySQL. By default, MySQL treats a backslash inside text as an escape character. So a value like C:\new needs care.

The JSON to SQL converter escapes quotes in values. It also quotes column names the way each dialect expects.

Do you load JSON from your own code? Then don’t glue values into the SQL text yourself. Use parameters instead. These are placeholders that the database driver fills in safely.

Parameters also protect you from SQL injection. This is an attack where someone sends special text that changes what your query does. Here is an example in Python with SQLite:

import json, sqlite3

rows = json.load(open("users.json"))
db = sqlite3.connect("app.db")
db.executemany(
    "INSERT INTO users (id, name, email, active) VALUES (?, ?, ?, ?)",
    [(r["id"], r["name"], r.get("email"), r["active"]) for r in rows],
)
db.commit()

Each driver uses its own placeholder. sqlite3 uses ?, but psycopg for PostgreSQL uses %s.

What do you do with nested JSON?

Real data is often not flat. Look at this row:

{ "id": 1, "name": "Ada", "address": { "city": "London", "zip": "N1 9GU" } }

You have two main choices.

Option 1: Flatten it into columns

Flattening turns nested keys into one-level names. So address.city becomes its own column.

The Flatten JSON tool does this, and you can choose the separator. The JSON to SQL converter also has a “Flatten nested” option. It gives you one column for each nested field.

Flatten when you often filter, sort or join on those fields. Columns are the fastest and simplest thing to query.

Lists of items are different, like the lines of an order. They usually go in a second table. That table has one row per item and a column that points back to the parent row.

Option 2: Store it in a JSON column

Most databases can store JSON in a column. You can still search inside it:

  • PostgreSQL has json and jsonb. Use jsonb in most cases. It is stored in a ready-to-use binary form. It is faster to query, and you can add an index to it. But it does not keep the key order, spaces or duplicate keys.
  • MySQL has a JSON type. It checks that the value is valid JSON when you insert it.
  • SQLite stores JSON as text. It has functions like json_extract() to read it.
  • SQL Server can store JSON in an NVARCHAR column. It reads it with functions such as JSON_VALUE and OPENJSON.

By default, the JSON to SQL converter stores nested values as JSON text. On PostgreSQL, it uses JSONB. Reading a value back looks like this:

-- PostgreSQL
SELECT address->>'city' FROM users;

-- MySQL
SELECT address->>'$.city' FROM users;

Choose a JSON column when the shape changes from row to row. It is also good when you mostly just need to keep the data.

Many tables use both. They have normal columns for the fields you search on, and a JSON column for the rest.

How do you insert many rows quickly?

One INSERT per row works, but it is slow for large files. A few simple habits help a lot:

  • Put rows in batches. Put many rows in one INSERT, like in Step 2. A good place to start is a few hundred to a few thousand rows per statement. SQL Server allows at most 1,000 rows in one VALUES list.
  • Use one transaction. Put BEGIN; before the inserts and COMMIT; after them. The database then saves everything at once, not after every statement. And if a row fails, nothing is half-loaded.
  • Use the bulk loader for really big files. PostgreSQL’s COPY and MySQL’s LOAD DATA are much faster than INSERT. They usually read CSV, so convert your JSON first. See JSON to CSV without losing data.

Common questions

Can PostgreSQL import a JSON file directly?

Yes, in a way. You can load the whole file into one jsonb value. Then turn the array into rows with functions such as jsonb_array_elements() or jsonb_to_recordset().

Can MySQL turn a JSON array into rows?

Yes. MySQL 8.0 has a JSON_TABLE() function. It turns a JSON array into a table. You can select from it or insert from it.

What is the difference between JSON and JSONB in PostgreSQL?

json stores the text exactly as you gave it. jsonb stores a ready-to-use version. It is faster to query and can have an index. But it drops the key order, spaces and duplicate keys.

Should I store JSON in a database at all?

It is fine for data that changes shape, or that you rarely search. For fields you often filter or join on, normal columns are simpler and faster.