Re: jsonb_set array append hack?

From: Andrew Dunstan <andrew(at)dunslane(dot)net>
To: Thom Brown <thom(at)linux(dot)com>, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: jsonb_set array append hack?
Date: 2015-09-14 18:59:33
Message-ID: 55F71915.5080708@dunslane.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On 09/14/2015 01:29 PM, Thom Brown wrote:
> Hi,
>
> I've noticed that if you use a string for an element key in jsonb_set
> with create_missing set to true, you can use it to append to an array:
>
> postgres=# SELECT jsonb_set(
> '{"name": "Joe", "vehicle_types": ["car","van"]}'::jsonb,
> '{vehicle_types,nonsense}',
> '"motorcycle"', true);
> jsonb_set
> ----------------------------------------------------------------
> {"name": "Joe", "vehicle_types": ["car", "van", "motorcycle"]}
> (1 row)
>
> What this really should match is a nested element inside
> "vehicle_types" called "nonsense". But this seems to be a hack to get
> an element added to an array. To do it properly currently requires
> specifying an arbitrary number in the hope that it will exceed the
> number of elements you have in the array.

That's a bug and we should fix it.

>
> e.g.
>
> postgres=# SELECT jsonb_set(
> '{"name": "Joe", "vehicle_types": ["car","van"]}'::jsonb,
> '{vehicle_types,100000}',
> '"motorcycle"', true);
> jsonb_set
> ----------------------------------------------------------------
> {"name": "Joe", "vehicle_types": ["car", "van", "motorcycle"]}
> (1 row)
>
> But I'm guessing people shouldn't be relying on the hack in the first
> example. Isn't this a bug? If so, wouldn't this also be a bug?:
>
> postgres=# SELECT jsonb_set(
> '{"name": "Joe", "vehicle_types": ["car","van"]}'::jsonb,
> array['vehicle_types',NULL],
> '"motorcycle"', true);
>
>

I think that's a bug too.

cheers

andrew

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Andrew Dunstan 2015-09-14 19:22:35 cache type info in json_agg and friends
Previous Message Teodor Sigaev 2015-09-14 18:57:53 Re: [PROPOSAL] Covering + unique indexes.