Skip to content

Instantly share code, notes, and snippets.

@simon-engledew
Created July 16, 2020 13:18
Show Gist options
  • Save simon-engledew/8113f785d3bbfbfa4d3596d8e4ce5845 to your computer and use it in GitHub Desktop.
Save simon-engledew/8113f785d3bbfbfa4d3596d8e4ce5845 to your computer and use it in GitHub Desktop.
Postgres || for json instead of jsonb
CREATE OR REPLACE FUNCTION json_merge_objects(a json, b json) RETURNS json
LANGUAGE SQL IMMUTABLE AS $$
WITH combined AS (
SELECT pair.key, pair.value FROM json_each(a) AS pair
UNION ALL
SELECT pair.key, pair.value FROM json_each(b) AS pair
)
SELECT json_object_agg(key, value)
FROM combined
$$;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment