pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
JSONB filed with default JSON from a file
6+ messages / 5 participants
[nested] [flat]

* JSONB filed with default JSON from a file
@ 2018-08-13 17:40  mrcasa bengaluru <mrcasablr@gmail.com>
  0 siblings, 1 reply; 6+ messages in thread

From: mrcasa bengaluru @ 2018-08-13 17:40 UTC (permalink / raw)
  To: pgsql-general

All,

I'm new to JSONB datatype. We would like to store a nested JSON file in
this field. Since the JSON is nested, we wanted to create JSON with default
value from an external JSON file.

My address table looks like,

CREATE TABLE address (
id CHAR(36) UNIQUE NOT NULL,
address JSONB NOT NULL
);

For example, the default JSON will look like,

$ cat address_default.json

{
  "address": {
    "address1": "175 N Street",
    "address2": "Timabktu",
    "location": [
      {
        "city": "Utopia",
        "geolocation": [
          {
            "lat": "12.345",
            "long": "12.1234"
          }
        ],
        "state": "Nowhere"
      }
    ],
    "zip": "96001"
  }
}


How do I make the address_default.json as the default JSON value for the
address column?

^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: JSONB filed with default JSON from a file
@ 2018-08-13 17:51  Charles Clavadetscher <clavadetscher@swisspug.org>
  parent: mrcasa bengaluru <mrcasablr@gmail.com>
  0 siblings, 1 reply; 6+ messages in thread

From: Charles Clavadetscher @ 2018-08-13 17:51 UTC (permalink / raw)
  To: mrcasa bengaluru <mrcasablr@gmail.com>; +Cc: pgsql-general

Hi

-----------------------------------
Charles Clavadetscher
Neugasse 84
CH - 8005 Zürich

Tel: +41-79-345 18 88
-------------------------------------

> On 13.08.2018, at 19:40, mrcasa bengaluru <mrcasablr@gmail.com> wrote:
> 
> All,
> 
> I'm new to JSONB datatype. We would like to store a nested JSON file in this field. Since the JSON is nested, we wanted to create JSON with default value from an external JSON file.
> 
> My address table looks like,
> 
> CREATE TABLE address (
> 	id			CHAR(36) UNIQUE NOT NULL,
> 	address		JSONB NOT NULL
> );
> 
> For example, the default JSON will look like,
> 
> $ cat address_default.json
> 
> {
>   "address": {
>     "address1": "175 N Street",
>     "address2": "Timabktu",
>     "location": [
>       {
>         "city": "Utopia",
>         "geolocation": [
>           {
>             "lat": "12.345",
>             "long": "12.1234"
>           }
>         ],
>         "state": "Nowhere"
>       }
>     ],
>     "zip": "96001"
>   }
> }
> 
> 
> How do I make the address_default.json as the default JSON value for the address column?
> 

I assume that you could declare the column as

address jsonb not null default 'your json here'::jsonb;

I did not try it, but this is what you would do with other data types.

Regards
Charles=

^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: JSONB filed with default JSON from a file
@ 2018-08-13 17:55  mrcasa bengaluru <mrcasablr@gmail.com>
  parent: Charles Clavadetscher <clavadetscher@swisspug.org>
  0 siblings, 1 reply; 6+ messages in thread

From: mrcasa bengaluru @ 2018-08-13 17:55 UTC (permalink / raw)
  To: Charles Clavadetscher <clavadetscher@swisspug.org>; +Cc: pgsql-general

>
> I assume that you could declare the column as
>
> address jsonb not null default 'your json here'::jsonb;
>

Thanks! However, this involves writing the entire JSON in the schema file
looks inconvenient. I was hoping I would be able to reference to an
external JSON file which could be used for the default value.

^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: JSONB filed with default JSON from a file
@ 2018-08-13 18:00  Merlin Moncure <mmoncure@gmail.com>
  parent: mrcasa bengaluru <mrcasablr@gmail.com>
  0 siblings, 1 reply; 6+ messages in thread

From: Merlin Moncure @ 2018-08-13 18:00 UTC (permalink / raw)
  To: mrcasablr@gmail.com; +Cc: Charles Clavadetscher <clavadetscher@swisspug.org>; pgsql-general

On Mon, Aug 13, 2018 at 12:56 PM mrcasa bengaluru <mrcasablr@gmail.com> wrote:
>>
>> I assume that you could declare the column as
>>
>> address jsonb not null default 'your json here'::jsonb;
>
>
> Thanks! However, this involves writing the entire JSON in the schema file looks inconvenient. I was hoping I would be able to reference to an external JSON file which could be used for the default value.

1) Stick the default json in a table somewhere, say default_json with
one row, one column
2) Wrap the table with a function, default_json() that returns the
value from the table
3) Make a default function for the table, DEFAULT default_json().
That way you externalize the default into the database

merlin




^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: JSONB filed with default JSON from a file
@ 2018-08-13 18:11  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Merlin Moncure <mmoncure@gmail.com>
  0 siblings, 1 reply; 6+ messages in thread

From: Tom Lane @ 2018-08-13 18:11 UTC (permalink / raw)
  To: Merlin Moncure <mmoncure@gmail.com>; +Cc: mrcasablr@gmail.com; Charles Clavadetscher <clavadetscher@swisspug.org>; pgsql-general

Merlin Moncure <mmoncure@gmail.com> writes:
> On Mon, Aug 13, 2018 at 12:56 PM mrcasa bengaluru <mrcasablr@gmail.com> wrote:
>> Thanks! However, this involves writing the entire JSON in the schema file looks inconvenient. I was hoping I would be able to reference to an external JSON file which could be used for the default value.

> [ put it in a table instead ]

Yeah.  If you really insist on having it in a file outside the database,
you can, but you'll need a superuser-privileged function to read it
from that file.  Aside from the security aspects, this sort of thing
is an antipattern because it opens you up to backup/restore problems
("oh, we needed that file too?"), replication problems, yadda yadda.
And what are you buying by doing it like that?  Better to keep it inside
the DB instead.

			regards, tom lane




^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: JSONB filed with default JSON from a file
@ 2018-08-13 18:52  Rob Sargent <robjsargent@gmail.com>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 6+ messages in thread

From: Rob Sargent @ 2018-08-13 18:52 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org



On 08/13/2018 12:11 PM, Tom Lane wrote:
> Merlin Moncure <mmoncure@gmail.com> writes:
>> On Mon, Aug 13, 2018 at 12:56 PM mrcasa bengaluru <mrcasablr@gmail.com> wrote:
>>> Thanks! However, this involves writing the entire JSON in the schema file looks inconvenient. I was hoping I would be able to reference to an external JSON file which could be used for the default value.
>
I'm struggling with the notion of default address.  Is the point to get 
a "blank" json structure in place.  Perhaps to fill in later? Otherwise, 
it seems like saying the default name is "Bob"? Rarely the value wanted.




^ permalink  raw  reply  [nested|flat] 6+ messages in thread


end of thread, other threads:[~2018-08-13 18:52 UTC | newest]

Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2018-08-13 17:40 JSONB filed with default JSON from a file mrcasa bengaluru <mrcasablr@gmail.com>
2018-08-13 17:51 ` Charles Clavadetscher <clavadetscher@swisspug.org>
2018-08-13 17:55   ` mrcasa bengaluru <mrcasablr@gmail.com>
2018-08-13 18:00     ` Merlin Moncure <mmoncure@gmail.com>
2018-08-13 18:11       ` Tom Lane <tgl@sss.pgh.pa.us>
2018-08-13 18:52         ` Rob Sargent <robjsargent@gmail.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox