Re: json accessors

From: Hannu Krosing <hannu(at)2ndQuadrant(dot)com>
To: Merlin Moncure <mmoncure(at)gmail(dot)com>
Cc: Andrew Dunstan <andrew(at)dunslane(dot)net>, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: json accessors
Date: 2012-11-29 01:07:52
Message-ID: 50B6B568.3080601@2ndQuadrant.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On 11/29/2012 01:10 AM, Merlin Moncure wrote:
> On Wed, Nov 28, 2012 at 2:44 PM, Andrew Dunstan <andrew(at)dunslane(dot)net> wrote:
...
>
>>> *) have you considered something like
>>> anyelement from_json(anyelement, json)
>>> or
>>> select <json>::some_type; (this may or many not be possible given our
>>> casting mechanics; i don't know).
>> I have no idea what the semantics of this would be.
> Yeah, there's a lot of nuance there.
One way to tackle it would give the argument element as a template
and the result will the same template filled in from json filled

create table tab1(id serial primary key, ts timestamp default now(),
data text);

insert into tab1 select from_json(row(null,null,null)::tab1,
'{"data":"the data"}');
insert into tab1 select from_json(row(null,null,null)::tab1, '{"id":-1,
"ts":null, "data":""}');
insert into tab1 select from_json(t.*,'{"data":"more data"}') from tab1
t where id = -1;

hannu=# select row_to_json(t.*) from tab1 t;
row_to_json
---------------------------------------------------------------
{"id":1,"ts":"2012-11-29 02:01:48.379172","data":"the data"}
{"id":-1,"ts":null, "data":""}
{"id":2,"ts":"2012-11-29 02:02:34.600164","data":"more data"}
(3 rows)

if extracting the defaults from table def proves too tricky for first
iteration, then
just set the missing fields to NULL or even better, carry over the
values from template;

------------------------------
Hannu

PS: good work so far :)

Hannu

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Hannu Krosing 2012-11-29 01:16:53 Re: json accessors
Previous Message Tom Lane 2012-11-29 00:30:01 Re: Bugs in CREATE/DROP INDEX CONCURRENTLY