Re: Returning values from an array of JSONB objects.

From: Vitaly Burovoy <vitaly(dot)burovoy(at)gmail(dot)com>
To: sighup <rts(at)sighup(dot)eu>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Returning values from an array of JSONB objects.
Date: 2016-04-13 10:53:35
Message-ID: CAKOSWN=hLyPLSTk5o7PL0giWLATPrXqYckusaiK6N5UAEtwEWQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 4/13/16, Vitaly Burovoy <vitaly(dot)burovoy(at)gmail(dot)com> wrote:
> On 4/13/16, sighup <rts(at)sighup(dot)eu> wrote:
>> Hi, please excuse either my stupidity or naivety regarding this but I'm
>> a bit confused. Give the following basic table structure :
>>
>> TABLE Data (
>> ID INT NOT NULL,
>> Markers jsonb NOT NULL
>> );
>>
>> And the following data:
>>
>> INSERT INTO Data (ID, Markers) VALUES(1, '[ {"idle": true, "items": 8,
>> "done": 0}, {"idle": true, "items": 8, "done": 0}]') ;

Prerequisite:
CREATE TYPE myrowtype AS (idle bool, items int, done int);

Couple examples:

>> How can I extract the value of the 'items' key either as two rows

SELECT (jsonb_populate_recordset(NULL::myrowtype, Markers)).items
FROM Data WHERE (ID = X);

>> and or a sum of both.

SELECT sum(items) FROM(
SELECT (jsonb_populate_recordset(NULL::myrowtype, Markers)).items
FROM Data WHERE (ID = X)
)AS t;

> You should use a proper function "jsonb_populate_recordset" [1].
>
> [1] http://www.postgresql.org/docs/9.5/static/functions-json.html

--
Best regards,
Vitaly Burovoy

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message sighup 2016-04-13 11:03:22 Re: Returning values from an array of JSONB objects.
Previous Message Vitaly Burovoy 2016-04-13 10:42:30 Re: Returning values from an array of JSONB objects.