Re: inserting json content from a file into table column - Mailing list pgsql-sql

From David G. Johnston
Subject Re: inserting json content from a file into table column
Date
Msg-id CAKFQuwYMnqeJtF7Abvk3ckK3f9bp7kxnT_TxHLX=9wh4XVUGPA@mail.gmail.com
Whole thread Raw
In response to Re: inserting json content from a file into table column  (Adrian Klaver <adrian.klaver@aklaver.com>)
Responses Re: inserting json content from a file into table column
List pgsql-sql
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.

pgsql-sql by date:

Previous
From: Adrian Klaver
Date:
Subject: Re: inserting json content from a file into table column
Next
From: Shashank Dutt Jha
Date:
Subject: Re: inserting json content from a file into table column