Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update JSON-type column with jsonb_set()

I tried to update data column (with type of json) of books table by jsonb_set

UPDATE books 
SET data = jsonb_set(data, '{borrowers}', '[{"name":"john doe","time":"1:00PM"}]')
WHERE id= 'some_id';

And encountered this error:

> ERROR:  function jsonb_set(json, unknown, unknown) does not exist

I found that in the doc, jsonb_set only work with jsonb type

jsonb_set(target jsonb, path text[], new_value jsonb [, create_missing boolean])

But I could not have privilege to update the column type to jsonb. So how could I resolve in this case?

like image 975
hgb123 Avatar asked May 09 '26 06:05

hgb123


1 Answers

Came up with a simple trick by type casting

UPDATE books 
SET data = jsonb_set(data::jsonb, '{borrowers}', '[{"name":"john doe","time":"1:00PM"}]')
WHERE id= 'some_id';
like image 136
hgb123 Avatar answered May 10 '26 21:05

hgb123



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!