The Example Data
All examples load this JSON array into a users table:
[
{ "id": 1, "name": "Emily Clark", "city": "London" },
{ "id": 2, "name": "James Smith", "city": "Leeds" }
]CREATE TABLE users ( id INTEGER PRIMARY KEY, name VARCHAR(100), city VARCHAR(100) );
Method 1: Generate INSERT Statements (Any Database)
The quickest way for a one-off import is to turn the JSON into plain SQL. Paste it into the JSON to SQL converter, pick MySQL, PostgreSQL, SQLite or SQL Server, and run the result:
INSERT INTO "users" ("id", "name", "city") VALUES
(1, 'Emily Clark', 'London'),
(2, 'James Smith', 'Leeds');It can also write the CREATE TABLE statement for you, with column types picked from your data. Good for test data, seed files and small exports.
Method 2: Let the Database Read the JSON
Modern databases understand JSON themselves, so you can insert straight from a JSON string without converting it first.
PostgreSQL
INSERT INTO users (id, name, city)
SELECT id, name, city
FROM json_populate_recordset(NULL::users,
'[{"id":1,"name":"Emily Clark","city":"London"},
{"id":2,"name":"James Smith","city":"Leeds"}]');json_populate_recordset matches the JSON keys to the table's column names, so the keys must be spelled the same way.
MySQL 8
SET @json = '[{"id":1,"name":"Emily Clark","city":"London"},
{"id":2,"name":"James Smith","city":"Leeds"}]';
INSERT INTO users (id, name, city)
SELECT jt.id, jt.name, jt.city
FROM JSON_TABLE(@json, '$[*]' COLUMNS (
id INT PATH '$.id',
name VARCHAR(100) PATH '$.name',
city VARCHAR(100) PATH '$.city'
)) AS jt;JSON_TABLE needs MySQL 8.0 or newer.
SQLite
INSERT INTO users (id, name, city)
SELECT json_extract(value, '$.id'),
json_extract(value, '$.name'),
json_extract(value, '$.city')
FROM json_each('[{"id":1,"name":"Emily Clark","city":"London"},
{"id":2,"name":"James Smith","city":"Leeds"}]');json_each turns every item of the array into a row, and json_extract picks a field out of it.
Method 3: Load It with a Script
For files you import regularly, a few lines of Python are the most reliable choice. This example uses SQLite, which is built into Python; the same pattern works with MySQL or PostgreSQL drivers.
import json, sqlite3
with open("users.json", encoding="utf-8") as f:
rows = json.load(f)
con = sqlite3.connect("app.db")
con.execute("CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT, city TEXT)")
con.executemany("INSERT INTO users (id, name, city) VALUES (:id, :name, :city)", rows)
con.commit()The :id, :name and :city placeholders let the driver escape every value safely, so quotes in names like O'Neil never break the query.
Or Store the Whole JSON in One Column
If the data has no fixed shape, keep it as JSON and query it later. PostgreSQL's jsonb type is the best choice for this:
CREATE TABLE events (id serial PRIMARY KEY, data jsonb);
INSERT INTO events (data) VALUES ('{"type": "signup", "user": {"city": "London"}}');
SELECT data->'user'->>'city' AS city FROM events; -- LondonMySQL has a JSON column type, and SQLite stores JSON as text that its JSON functions can read.
Tips
- Validate first. One trailing comma stops the whole import. Check the file with the JSON Validator, or fix it with JSON Repair.
- Flatten nested objects into columns like
address_city, or keep them in a JSON column. - Use a transaction for big files, so a failed row does not leave half the data imported.
- Never build SQL by gluing strings together from user data; use placeholders as shown above.
- Need the opposite? Turn INSERT statements back into JSON with SQL to JSON.
Frequently Asked Questions
What is the easiest way to import JSON into a database?
For a one-off import, paste the JSON into our free JSON to SQL converter and run the INSERT statements it generates. For regular imports, use a short script or your database's JSON functions.
Can PostgreSQL read JSON directly?
Yes. json_populate_recordset turns a JSON array into rows that match a table, and the jsonb column type stores JSON that you can query with operators like -> and ->>.
How do I import JSON into MySQL?
In MySQL 8 or newer, use JSON_TABLE to turn a JSON array into rows, then INSERT ... SELECT from it. You can also store whole documents in a JSON column.
How do I handle nested JSON objects?
Either flatten them into separate columns, such as address_city, or keep the nested part in a JSON or jsonb column and query it later.