Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Update JSONB with UUID value

I have a table with a column of jsonb type, the JSON object is of key:value pairs, the problem is, I need to update one of the keys to contain a UUID instead of the original value.

The update statement I’m using:

UPDATE
    public.applications a
SET
    data = jsonb_set(data, '{id}', (SELECT b.uuid FROM public.devices b WHERE b.id = (a.data ->> 'id')::integer)::text)

Postgres returns the following error:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

ERROR:  function jsonb_set(jsonb, unknown, text) does not exist

I’ve tried to cast the value to different data types, but with same result.

Sample data

id | data
---------
1  | {"id": 1}
2  | {"id": 2}

Expected output

id | data
---------
1  | {"id": device_uuid_here}
2  | {"id": device_uuid_here}

DB_VERSION: PostgreSQL12.12

>Solution :

UPDATE public.applications a
SET data = jsonb_set(data, '{id}', 
  (SELECT '"'||b.uuid::text||'"' FROM public.devices b 
  WHERE b.id = (a.data ->> 'id')::integer)::jsonb);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading