I got a json type column named 'attr' which was used to store a list include some dict like [{"foo":1}, {"foo":2}, {"foo":3}].
Now I want to extend the list to [{"foo":1}, {"foo":2}, {"foo":3}, {"foo":4}]. Of course I can use a sql query like:
update tbl
set attr='[{"foo":1}, {"foo":2}, {"foo":3}, {"foo":4}]'::json;
However, I need execute it in my webserver. So I must select the former value, extend the list and then update into table.
I'm thinking if there is a way to use some postgresql function to do this like array_to_json or json_array_elements
I wrote this query:
update tbl
set attr = json_build_array(json_array_elements(attr), '{"foo":4}'::json);
But it got wrong result.