Thread: inserting json content from a file into table column

inserting json content from a file into table column

From
Shashank Dutt Jha
Date:
I have .json file.C:/ a.json
Table with column 'food' of type jsonb

how to insert the content of a.json into column 'food'

Re: inserting json content from a file into table column

From
Adrian Klaver
Date:
On 02/10/2016 07:10 AM, Shashank Dutt Jha wrote:
> I have .json file.C:/ a.json
> Table with column 'food' of type jsonb
>
> how to insert the content of a.json into column 'food'

What are you using as your client, for example psql, Java program, 
Python program, etc.?

What version of Postgres are you using? In this case it probably does 
not matter that much, but json/jsonb has changed a good deal over recent 
versions so it is nice to know what you are working with.


-- 
Adrian Klaver
adrian.klaver@aklaver.com



Re: inserting json content from a file into table column

From
Shashank Dutt Jha
Date:
<div dir="ltr">psql. PostgreSQL v9.5</div><div class="gmail_extra"><br /><div class="gmail_quote">On Wed, Feb 10, 2016
at8:52 PM, Adrian Klaver <span dir="ltr"><<a href="mailto:adrian.klaver@aklaver.com"
target="_blank">adrian.klaver@aklaver.com</a>></span>wrote:<br /><blockquote class="gmail_quote" style="margin:0 0 0
.8ex;border-left:1px#ccc solid;padding-left:1ex"><div class="HOEnZb"><div class="h5">On 02/10/2016 07:10 AM, Shashank
DuttJha wrote:<br /><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc
solid;padding-left:1ex">I have .json file.C:/ a.json<br /> Table with column 'food' of type jsonb<br /><br /> how to
insertthe content of a.json into column 'food'<br /></blockquote><br /></div></div> What are you using as your client,
forexample psql, Java program, Python program, etc.?<br /><br /> What version of Postgres are you using? In this case
itprobably does not matter that much, but json/jsonb has changed a good deal over recent versions so it is nice to know
whatyou are working with.<span class="HOEnZb"><font color="#888888"><br /><br /><br /> -- <br /> Adrian Klaver<br /><a
href="mailto:adrian.klaver@aklaver.com"target="_blank">adrian.klaver@aklaver.com</a><br
/></font></span></blockquote></div><br/></div> 

Re: inserting json content from a file into table column

From
Adrian Klaver
Date:
On 02/10/2016 08:01 AM, Shashank Dutt Jha wrote:
> psql. PostgreSQL v9.5

A quick and dirty method:

aklaver@test=> create table json_test(id integer, json_fld jsonb);
CREATE TABLE

aklaver@test=> \e sample.json

\e opens an editor. Inside editor add INSERT statement to file:
 INSERT INTO json_test VALUES(1, '
{    "glossary": {        "title": "example glossary",        "GlossDiv": {            "title": "S",
"GlossList":{                "GlossEntry": {                    "ID": "SGML",                    "SortAs": "SGML",
             "GlossTerm": "Standard Generalized Markup Language",                    "Acronym": "SGML",
  "Abbrev": "ISO 8879:1986",                    "GlossDef": {                        "para": "A meta-markup language,
usedto create 
 
markup languages such as DocBook.",                        "GlossSeeAlso": ["GML", "XML"]                    },
          "GlossSee": "markup"                }            }        }    }
 
}');


aklaver@test=> \x
Expanded display is on.
aklaver@test=> select * from json_test;
-[ RECORD 1 

]-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
id       | 1
json_fld | {"glossary": {"title": "example glossary", "GlossDiv": 
{"title": "S", "GlossList": {"GlossEntry": {"ID": "SGML", "Abbrev": "ISO 
8879:1986", "SortAs": "SGML", "Acronym": "SGML", "GlossDef": {"para": "A 
meta-markup language, used to create markup languages such as DocBook.", 
"GlossSeeAlso": ["GML", "XML"]}, "GlossSee": "markup", "GlossTerm": 
"Standard Generalized Markup Language"}}}}}


Otherwise you will need to find a way to pass the file in from the 
shell. You are using Windows it seems and I am not familiar enough with 
it to offer any guidance on that topic.


>
> On Wed, Feb 10, 2016 at 8:52 PM, Adrian Klaver
> <adrian.klaver@aklaver.com <mailto:adrian.klaver@aklaver.com>> wrote:
>
>     On 02/10/2016 07:10 AM, Shashank Dutt Jha wrote:
>
>         I have .json file.C:/ a.json
>         Table with column 'food' of type jsonb
>
>         how to insert the content of a.json into column 'food'
>
>
>     What are you using as your client, for example psql, Java program,
>     Python program, etc.?
>
>     What version of Postgres are you using? In this case it probably
>     does not matter that much, but json/jsonb has changed a good deal
>     over recent versions so it is nice to know what you are working with.
>
>
>     --
>     Adrian Klaver
>     adrian.klaver@aklaver.com <mailto:adrian.klaver@aklaver.com>
>
>


-- 
Adrian Klaver
adrian.klaver@aklaver.com



Re: inserting json content from a file into table column

From
"David G. Johnston"
Date:
On Wed, Feb 10, 2016 at 9:34 AM, Adrian Klaver <adrian.klaver@aklaver.com> wrote:
On 02/10/2016 08:01 AM, Shashank Dutt Jha wrote:
psql. PostgreSQL v9.5

A quick and dirty method:

aklaver@test=> create table json_test(id integer, json_fld jsonb);
CREATE TABLE

aklaver@test=> \e sample.json

\e opens an editor. Inside editor add INSERT statement to file:

 INSERT INTO json_test VALUES(1, '
{
    "glossary": {
        "title": "example glossary",
        "GlossDiv": {
            "title": "S",
            "GlossList": {
                "GlossEntry": {
                    "ID": "SGML",
                    "SortAs": "SGML",
                    "GlossTerm": "Standard Generalized Markup Language",
                    "Acronym": "SGML",
                    "Abbrev": "ISO 8879:1986",
                    "GlossDef": {
                        "para": "A meta-markup language, used to create markup languages such as DocBook.",
                        "GlossSeeAlso": ["GML", "XML"]
                    },
                    "GlossSee": "markup"
                }
            }
        }
    }
}');


​I'd suggest dollar-quoting if going this route - the possibility of the json containing single quotes is significantly large to warrant it.

[...] VALUES(1,$$json$
{...}
$json$);

In Linux we'd do:

(backticks used below)
\set json_var `cat json_file.txt`
INSERT INTO tbl (json_col) VALUES (:'json_var');

​Not sure about Windows though​.

David J.

Re: inserting json content from a file into table column

From
Shashank Dutt Jha
Date:
I am trying to insert directly into table using pgAdmin tool.

I came across something like this

create temporary table temp_json (values jsonb) on commit drop;
copy temp_json from 'C:\Users\\conceptmaps.json';

insert into jsontest ('food') from ---*
//
sp copy seeme to have copied the contents from conceptmaps.json ( from message displayed)
now how to store that content into column 'food' 

On Wed, Feb 10, 2016 at 10:20 PM, David G. Johnston <david.g.johnston@gmail.com> wrote:
On Wed, Feb 10, 2016 at 9:34 AM, Adrian Klaver <adrian.klaver@aklaver.com> wrote:
On 02/10/2016 08:01 AM, Shashank Dutt Jha wrote:
psql. PostgreSQL v9.5

A quick and dirty method:

aklaver@test=> create table json_test(id integer, json_fld jsonb);
CREATE TABLE

aklaver@test=> \e sample.json

\e opens an editor. Inside editor add INSERT statement to file:

 INSERT INTO json_test VALUES(1, '
{
    "glossary": {
        "title": "example glossary",
        "GlossDiv": {
            "title": "S",
            "GlossList": {
                "GlossEntry": {
                    "ID": "SGML",
                    "SortAs": "SGML",
                    "GlossTerm": "Standard Generalized Markup Language",
                    "Acronym": "SGML",
                    "Abbrev": "ISO 8879:1986",
                    "GlossDef": {
                        "para": "A meta-markup language, used to create markup languages such as DocBook.",
                        "GlossSeeAlso": ["GML", "XML"]
                    },
                    "GlossSee": "markup"
                }
            }
        }
    }
}');


​I'd suggest dollar-quoting if going this route - the possibility of the json containing single quotes is significantly large to warrant it.

[...] VALUES(1,$$json$
{...}
$json$);

In Linux we'd do:

(backticks used below)
\set json_var `cat json_file.txt`
INSERT INTO tbl (json_col) VALUES (:'json_var');

​Not sure about Windows though​.

David J.