postgresql - Postgres plpgsql JSON assignment -
i'm working jsonb in postgres , trying understand how correctly perform assignment json property in plpgsql.
this query snippet reports syntax error because of leading bracket on assignment, sure syntax required refer json objects in plpgsq:
if (new."data")->>'custom' null (new."data")->>'custom' := 0; end if;
this in postgresql trigger, new provided variable relating new database record.
can advise correct technique assigning value json(b) property?
in postgres 9.5 quite simple function jsonb_set()
:
if new.data->>'custom' null new.data = jsonb_set(new.data::jsonb, '{custom}', '0'::jsonb); end if;
there no jsonb_set()
in postgres 9.4 issue more complicated:
if new.data->>'custom' null new.data = ( select json_object_agg(key, value) ( select key, value jsonb_each(new.data) union values ('custom', '0'::jsonb) ) s ); end if;
Comments
Post a Comment