Skip to content
Advertisement

how to update value of key in json type field on PostgreSQL

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'

Demo on DB Fiddle:

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" }'
)
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement