You can feed the JSON into a SQL statement that extracts the information and inserts that into the table. If the JSON attributes have exactly the name as the table columns you can do something like this:
with customer_json (doc) as (
values
('[
{
"id": 23635,
"name": "Jerry Green",
"comment": "Imported from facebook."
},
{
"id": 23636,
"name": "John Wayne",
"comment": "Imported from facebook."
}
]'::json)
)
insert into customer (id, name, comment)
select p.*
from customer_json l
cross join lateral json_populate_recordset(null::customer, doc) as p
on conflict (id) do update
set name = excluded.name,
comment = excluded.comment;
New customers will be inserted, existing ones will be updated. The "magic" part is the json_populate_recordset(null::customer, doc) which generates a relational representation of the JSON objects.
The above assumes a table definition like this:
create table customer
(
id integer primary key,
name text not null,
comment text
);
If the data is provided as a file, you need to first put that file into some table in the database. Something like this:
create unlogged table customer_import (doc json);
Then upload the file into a single row of that table, e.g. using the \copy command in psql (or whatever your SQL client offers):
\copy customer_import from 'customers.json' ....
You may have to make it one line:
COPY asn_temp FROM PROGRAM 'curl https://raw.githubusercontent.com/ipverse/asn-ip/master/as/1234/aggregated.json | jq -rc "."'
Then you can use the above statement, just remove the CTE and use the staging table:
insert into customer (id, name, comment)
select p.*
from customer_import l
cross join lateral json_populate_recordset(null::customer, doc) as p
on conflict (id) do update
set name = excluded.name,
comment = excluded.comment;
Another example to insert mutiple rows by unpacking an array
INSERT INTO asn_ips (asn, ip)
SELECT * FROM (
-- get the 'asn' key
SELECT json_extract_path_text(data, 'asn')::int as asn,
-- get the 'subnets'.'ipv6' sub key and remove quotes with json_array_elements_text
json_array_elements_text(json_extract_path_text(data, 'subnets', 'ipv6')::json)::cidr as ip
FROM public.asn_temp
UNION
-- get the 'asn' key
SELECT json_extract_path_text(data, 'asn')::int as asn,
-- get the 'subnets'.'ipv4' sub key and remove quotes with json_array_elements_text
json_array_elements_text(json_extract_path_text(data, 'subnets', 'ipv4')::json)::cidr as ip
FROM public.asn_temp
) as ips GROUP BY asn, ip;