Thread: Working with JSONB data having node lists

Working with JSONB data having node lists

From
geoff hoffman
Date:
JSONB fields are very attractive for our current use, particularly as straight key-value pairs in the JSONB data;

but we are having trouble finding documentation on how to query lists (of scalars or objects) in nodes of the JSONB
data.

~~~

I have the table as follows:

CREATE TABLE public.contacts
(
    id integer NOT NULL DEFAULT nextval('contacts_id_seq'::regclass),
    uuid uuid NOT NULL DEFAULT gen_random_uuid(),
    vertical_id integer NOT NULL,
    inboundlog_id integer NOT NULL,
    email character varying(255) COLLATE pg_catalog."default" NOT NULL,
    data jsonb NOT NULL,
    created_at timestamp(0) without time zone,
    updated_at timestamp(0) without time zone,
    CONSTRAINT contacts_pkey PRIMARY KEY (id)
)
WITH (
    OIDS = FALSE
)
TABLESPACE pg_default;

~~~

I have a record as follows:

INSERT INTO contacts
(uuid, vertical_id, inboundlog_id, email, data)
VALUES
(gen_random_uuid(), 1, 1, ‘phil@site.com',
 '{"first”:"Phil","last”:"Peters”,"subscriptions”:[101,202,303]}')


How do I craft a query to find all subscribers to program 202?

~~~

I have another record as follows:

INSERT INTO contacts
(uuid, vertical_id, inboundlog_id, email, data)
VALUES
(gen_random_uuid(), 1, 1, ‘bob@domain.com',
 '{"first”:"Bob","last”:"Baker”,"downloads":[{"date":"2018-01-01 00:00:00","pubid”:123},{"date":"2018-02-02
00:00:00","pubid”:456}]}')

How do I craft a query to find all contacts who have downloaded pubid 123?

TIA -
Geoff




Re: Working with JSONB data having node lists

From
"David G. Johnston"
Date:
On Tue, Jan 30, 2018 at 2:47 PM, geoff hoffman <geoff@rxmg.com> wrote:
JSONB fields are very attractive for our current use, particularly as straight key-value pairs in the JSONB data;

but we are having trouble finding documentation on how to query lists (of scalars or objects) in nodes of the JSONB data.

 
 '{"first”:"Phil","last”:"Peters”,"subscriptions”:[101,202,303]}')
​​


How do I craft a query to find all subscribers to program 202?

​​SELECT ​data->'subscriptions' ? '202'​

The docs speak of "top-level keys" but that also includes array element values.
 
 '{"first”:"Bob","last”:"Baker”,"downloads":[{"date":"2018-01-01 00:00:00","pubid”:123},{"date":"2018-02-02 00:00:00","pubid”:456}]}')

How do I craft a query to find all contacts who have downloaded pubid 123?

If you can do this one without unnesting the downloads array I do not know how.  Having done that: (WHERE dlarray->'publd' = '123)

David J.

RE: Working with JSONB data having node lists

From
Steven Winfield
Date:

If you can do this one without unnesting the downloads array I do not know how.  Having done that: (WHERE dlarray->'publd' = '123)

 

I think it’s doable like this:

 

select * from contacts where data @> '{"downloads":[{"pubid":123}]}'::jsonb

 

…which would be aided by a gin index on ‘data’ using jsonb_path_ops:

 

CREATE INDEX idx_data_path_ops on contacts USING gin (data jsonb_path_ops);

https://www.postgresql.org/docs/10/static/datatype-json.html#JSON-INDEXING

 

Best,

Steve.





This email is confidential. If you are not the intended recipient, please advise us immediately and delete this message. The registered name of Cantab- part of GAM Systematic is Cantab Capital Partners LLP. See - http://www.gam.com/en/Legal/Email+disclosures+EU for further information on confidentiality, the risks of non-secure electronic communication, and certain disclosures which we are required to make in accordance with applicable legislation and regulations. If you cannot access this link, please notify us by reply message and we will send the contents to you.