0

I'm trying to import json file into a PostgreSQL. Example data:

 {
  "asin":"2094869245",
  "title":"5 LED Bicycle Rear Tail Red Bike Torch Laser Beam Lamp Light",
  "price":8.26,
  "imhUrl":"http://ecx.images-amazon.com/images/I/51RtwnJwtBL._SY300_.jpg"
 }

 {
  "asin":"7245456259",
  "title":"Black Mountain Products Single Resistance Band - Door Anchor,
  "price":10.49,
  "imhUrl":"http://ecx.images-amazon.com/images/I/411Ikpf122L._SY300_.jpg"
 }`

Would like the result to look like:

data
--------------------------------------------------------------------
{
  "asin":"2094869245",
  "title":"5 LED Bicycle Rear Tail Red Bike Torch Laser Beam Lamp Light",
  "price":8.26,
  "imhUrl":"http://ecx.images-amazon.com/images/I/51RtwnJwtBL._SY300_.jpg"
}

--------------------------------------------------------------------
{
  "asin":"7245456259",
  "title":"Black Mountain Products Single Resistance Band - Door Anchor,
  "price":10.49,
  "imhUrl":"http://ecx.images-amazon.com/images/I/411Ikpf122L._SY300_.jpg"
}

The data is type json.

My JSON FILE will be stored in a single JSON column called data.

7
  • So the file contains invalid JSON and you want to split it up in valid JSON values? Commented Jul 2, 2019 at 10:57
  • NO.i have json file valid and i want to import him in a single json column in postgres. Commented Jul 2, 2019 at 11:04
  • Your first example is an invalid JSON value. Is that supposed to be two files or just one file? If it's one file, then how are the individual objects separated? Commented Jul 2, 2019 at 11:05
  • See e.g. here and here Commented Jul 2, 2019 at 11:09
  • 1
    Then your example is missing a , between the values - as shown, this is invalid JSON. Commented Jul 2, 2019 at 11:11

1 Answer 1

1

if you remove the newline in your JSON file like this:

{ "asin":"2094869245", "title":"5 LED Bicycle Rear Tail Red Bike Torch Laser Beam Lamp Light","price":8.26,  "imhUrl":"http://ecx.images-amazon.com/images/I/51RtwnJwtBL._SY300_.jpg"}
{ "asin":"7245456259",  "title":"Black Mountain Products Single Resistance Band - Door Anchor",  "price":10.49,  "imhUrl":"http://ecx.images-amazon.com/images/I/411Ikpf122L._SY300_.jpg" }

you can load to a table with copy command:

create table js (a json);

copy js from '/tmp/data.json'  DELIMITER '^'  CSV  QUOTE ''''  ESCAPE '\'
Sign up to request clarification or add additional context in comments.

1 Comment

very helpful @anthony-sotolongo. worked perfectly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.