Skip to content

Instantly share code, notes, and snippets.

@ianthrive
Created February 2, 2016 14:07
Show Gist options
  • Save ianthrive/9369d58c435dca9342ae to your computer and use it in GitHub Desktop.
Save ianthrive/9369d58c435dca9342ae to your computer and use it in GitHub Desktop.
CREATE OR REPLACE FUNCTION json_diff(l JSONB, r JSONB) RETURNS JSONB LANGUAGE sql AS $$
SELECT jsonb_object_agg(a.key, a.value) FROM
( SELECT key, value FROM jsonb_each(l) ) a LEFT OUTER JOIN
( SELECT key, value FROM jsonb_each(r) ) b ON a.key = b.key
WHERE a.value != b.value OR b.key IS NULL;
$$;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment