I am running postgresql 9.6 version. I am storing data in json type field.
insert into testable (name, jsonvalues) values("Jacky", "{'has_attachment': True, 'flag':'True'")
I want to update “flag” to “False”.
What query i have to use?
Advertisement
Answer
Here is one way to do it:
update testable set jsonvalues = jsonb_set(jsonvalues::jsonb, '{flag}', '"False"')::json where name = 'Jacky'
name | jsonvalues :---- | :------------------------------------------ Jacky | {"flag": "False", "has_attachment": "True"}
Note that your original insert
query is not valid Postgres syntax. You need single quotes around the values, and double quotes within the JSON, so that should be:
insert into testable (name, jsonvalues) values( 'Jacky', '{"has_attachment": "True", "flag":"True" }' )