JSON Converter · Tutorial
How to import a JSON file into a PostgreSQL table
8 min readintermediateUpdated
You have product data as a JSON array, maybe exported from an API or a no-code tool, and you want it in a PostgreSQL table so you can query and join it. This tutorial generates a ready-to-run SQL script, explains each choice the generator makes, and shows how to load the script with psql.
What you need
- The JSON Converter with SQL INSERT chosen under Convert to
- A JSON array of objects (the Try it button loads three products)
- A PostgreSQL database you can connect to, and the
psqlcommand-line client or a GUI such as pgAdmin
Steps
Paste the JSON and pick PostgreSQL
Paste the array into the left box, pick SQL INSERT under Convert to, choose PostgreSQL as the database and type
productsas the table name. The SQL appears immediately, with a message saying it found 3 rows and 7 columns.Review the inferred column types
Look at the CREATE TABLE statement.
idbecame INTEGER,priceDOUBLE PRECISION (because one price has decimals),inStockBOOLEAN,addedAtTIMESTAMPTZ (because the values are ISO 8601 timestamps), andtagsJSONB.colorappears in only one product, so it's the only nullable column.CREATE TABLE "products" ( "id" INTEGER NOT NULL, "name" TEXT NOT NULL, "price" DOUBLE PRECISION NOT NULL, "inStock" BOOLEAN NOT NULL, "addedAt" TIMESTAMPTZ NOT NULL, "tags" JSONB NOT NULL, "color" TEXT );Adjust the table if needed
The generated table is a draft. For real use you'll usually want a primary key, so edit it after downloading, for example by changing the id line to
"id" INTEGER PRIMARY KEY. For money,NUMERIC(10,2)is often a better type than DOUBLE PRECISION.Download and run the script
Press Download .sql to save
products.sql, then run it with psql. The-v ON_ERROR_STOP=1flag stops at the first error instead of carrying on.psql "postgresql://user@localhost/shop" -v ON_ERROR_STOP=1 -f products.sqlCheck the data
Query the table to confirm it worked. Because
tagsis JSONB, you can query inside it straight away, for example to find every lighting product:SELECT name FROM products WHERE tags ? 'lighting';
Try it yourself
Opens the JSON Converter with this tutorial's sample loaded.
Common mistakes
- Running CREATE TABLE twice
- If the table already exists, untick Include CREATE TABLE and generate only the INSERT statements.
- Mixed-case column names
- Quoted identifiers keep their case in PostgreSQL, so
"inStock"must always be written with quotes in later queries. Rename keys to snake_case in the JSON first if you prefer unquoted names. - Very large imports
- For hundreds of thousands of rows, PostgreSQL's
COPYcommand is much faster than INSERT statements. Use this tool for small to medium imports, seeds and test data.
Next steps
Go back to the JSON Converter, or keep going with one of these:
FAQ
Can I import into MySQL or SQLite instead?
Yes. Choose the database from the list and the quoting, types and literals change to match it.
What if some objects have extra fields?
Every key that appears in any object becomes a column. Objects without it get NULL in that column.