Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update an empty jsonb column using jsonb_set in postgresql?

My purpose is to update a jsonb column using jsonb_set, which is currently null, with an object having more than one key-value pairs. The update command executes successfully but it is not updating anything, the column is still coming up empty. I am trying the following query.

UPDATE tab 
  set value = jsonb_set(value, '{}', '{"a" : 100, "b" : [100, 200]}'::jsonb) 
where id = 100;

Any solutions ?

like image 705
JessePinkman Avatar asked May 14 '26 16:05

JessePinkman


1 Answers

From what I've understood, it appears you don't need jsonb_set for this case. Simply cast the string to jsonb for updating

UPDATE tab 
  set value = '{"a" : 100, "b" : [100, 200]}'::jsonb
where id = 100 
--and value is null; --additional check if you need.

Demo

like image 130
Kaushik Nayak Avatar answered May 16 '26 14:05

Kaushik Nayak