Please note that only the first case is not supported in jsonb_set(), which was a very important addition to PostgreSQL 9.5. If this case would be added, a simple call with the same path '{boo,baz}' and '"newvalue"' would always be sufficient. Goal: first path level (boo) does not exist, create subelement, but if there is a second path level (baz) requested, create a subdict instead of just the given value (newvalue). -- Creating a new 2nd-level dict with a missing 1st-level key/dict just in the path does not work SELECT jsonb_set('{"foo": "bar"}'::jsonb, '{boo,baz}', '"newvalue"'); jsonb_set ---------------- {"foo": "bar"} -- Only with the complete subdict as value the new 1st-level key 'boo' is added(so the caller has to know if the key is missing) SELECT jsonb_set('{"foo": "bar"}'::jsonb, '{boo}', '{"baz": "newvalue"}'); jsonb_set -------------------------------------------- {"boo": {"baz": "newvalue"}, "foo": "bar"} -- All other cases work fine (when the 1st-level key 'boo' already exists) SELECT jsonb_set('{"foo": "bar", "boo": {"otherkey": "othervalue"}}'::jsonb, '{boo,baz}', '"newvalue"'); jsonb_set ---------------------------------------------------------------------- {"boo": {"baz": "newvalue", "otherkey": "othervalue"}, "foo": "bar"} SELECT jsonb_set('{"foo": "bar", "boo": {"baz": "oldvalue", "otherkey": "othervalue"}}'::jsonb, '{boo,baz}', '"newvalue"'); jsonb_set ---------------------------------------------------------------------- {"boo": {"baz": "newvalue", "otherkey": "othervalue"}, "foo": "bar"} -- Sent via pgsql-general mailing list (pgsql-general@xxxxxxxxxxxxxx) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-general