Can't wait to use the new JSONB operators of 9.5, e.g.:
SELECT '{"k1":"v1"}'::JSONB || '{"k1":"v2","k2":true}'::JSONB
=> {"k1": "v2", "k2": true}In 9.4 this is the 'best way' I know:
SELECT
('{' || STRING_AGG(
'"' || COALESCE(j2.key, j1.key) ||
'": ' || TO_JSON(COALESCE(j2.value, j1.value)
), ',') || '}')::JSONB
FROM JSONB_EACH('{"k1":"v1"}') j1
FULL OUTER JOIN
(SELECT * FROM JSONB_EACH('{"k1":"v2","k2":true}')) j2 ON j1.key = j2.key