Skip to content

Convert JSON to PostgreSQL

Drop your JSON file to get a PostgreSQL script with CREATE TABLE and INSERT statements. Each column gets a type, and every fix is listed with its row and column.

Your file
jsonpedidos_2024.jsonSample file, 771 B
Your database

4 rows converted, 2 fixes, none rejected

Show
The first rows of pedidos_2024.json, with each fix marked
Row
2000481Peña Muñoz2024-02-03T10:15:00Z1234.5true
3000482José Ángel Ruiz2024-02-15T16:40:00Z99.9falseEntrega en la tarde
4000483Zoë Ortíz2024-02-29T09:05:00Z12true
5000484Ñuño Castañeda2024-03-11T13:30:00Z980.75síFactura con "RFC"
-- Written by IntoSQL for PostgreSQL.

SET client_encoding = 'UTF8';
BEGIN;

CREATE TABLE pedidos_2024 (
    pedido text NOT NULL,
    cliente text NOT NULL,
    fecha timestamptz NOT NULL,
    total numeric(6,2) NOT NULL,
    pagado boolean NOT NULL,
    notas text
);

INSERT INTO pedidos_2024 (pedido, cliente, fecha, total, pagado, notas) VALUES
('000481', 'Peña Muñoz', '2024-02-03 10:15:00+00:00', 1234.5, TRUE, NULL),
('000482', 'José Ángel Ruiz', '2024-02-15 16:40:00+00:00', 99.9, FALSE, 'Entrega en la tarde'),
('000483', 'Zoë Ortíz', '2024-02-29 09:05:00+00:00', 12, TRUE, NULL),
('000484', 'Ñuño Castañeda', '2024-03-11 13:30:00+00:00', 980.75, TRUE, 'Factura con "RFC"');

COMMIT;

Fixes

  1. Leading zeros kept in "pedido": 000481 stays 000481, stored as text.
  2. "pagado" holds yes and no words (sí), stored as boolean.

Your file is converted on this computer and never uploaded.

Other samples:

From JSON to PostgreSQL, column by column

The sample above, pedidos_2024.json, as IntoSQL writes it for PostgreSQL: what each column holds in the file, the type it gets and how its first value lands in the script.

In the fileFirst valueColumnPostgreSQL typeIn the script
pedido000481pedidotext'000481'
clientePeña Muñozclientetext'Peña Muñoz'
fecha2024-02-03T10:15:00Zfechatimestamptz'2024-02-03 10:15:00+00:00'
total1234.5totalnumeric(6,2)1234.5
pagadotruepagadobooleanTRUE
notas-notastextNULL

Load the script into PostgreSQL

The download is a plain .sql file. Run it with PostgreSQL's own tools.

From a terminal

psql -v ON_ERROR_STOP=1 -d mydb -f pedidos_2024.postgresql.sql

ON_ERROR_STOP makes psql stop at the first error. The script runs in one transaction, so a failed load leaves the database as it was.

From a database tool

In pgAdmin or DBeaver, open the script in a query window and run it.

Without a scriptPro

IntoSQL Pro loads the file straight into PostgreSQL from the desktop app or the command line, without writing a script.

intosql load pedidos_2024.json --into postgres://user@localhost/mydb

What to know about JSON files and PostgreSQL

What IntoSQL reads in JSON files

  • The file can be an array of objects, an object that holds that array under a key such as data, or one object per line (NDJSON).
  • Each key becomes a column. Objects and arrays inside a value are kept as JSON text.
  • Numbers keep their exact digits, and ISO 8601 dates become dates or timestamps.

What to know about PostgreSQL

  • Decimals keep their exact digits as numeric(p,s), and dates with an offset become timestamptz.
  • PostgreSQL text can't hold the NUL character, so it is removed from the values that have one.
  • The script runs in one transaction, so a load that fails leaves nothing half done.

How each kind of value is stored in PostgreSQL

ValuePostgreSQL
Yes and noboolean
Whole numbersinteger
Large whole numbersbigint
Decimalsnumeric(p,s)
Floating-point numbersdouble precision
Datesdate
Times of daytime
Dates with a timetimestamp
Dates with a time and offsettimestamptz
Texttext

Questions about JSON to PostgreSQL

Not answered here? Write to us from the contact page and a person replies. Contact page

How do I convert JSON to PostgreSQL?

Drop your file on the converter at the top of this page. It finds the header, reads every value and writes the PostgreSQL script, listing each fix it made. Download the script and run it with psql.

What shape does the JSON file need?

An array of objects, an object that holds that array under a key such as data, or one object per line (NDJSON). Each key becomes a column.

Which PostgreSQL types does it use?

Yes and no values become boolean, whole numbers integer, decimals numeric(p,s), dates date, dates with a time timestamp and text text. Sizes come from the longest value in each column.

Does it keep leading zeros in PostgreSQL?

Yes. A code such as 000481 stays text, so PostgreSQL stores it as text and the script writes it as '000481'.

Can it load the data into PostgreSQL without a script?

Yes, with IntoSQL Pro. The desktop app and the command line connect to PostgreSQL and load the rows straight in, creating the table first.

How big can the JSON file be?

NDJSON, one object per line, is read as it streams in, so files of several gigabytes work in the browser. A JSON document is read whole, so its limit is the memory the browser gives this page. The desktop app and the command line take files of any size.