Skip to content

Instantly share code, notes, and snippets.

@emiel
Created May 24, 2017 09:39
Show Gist options
  • Save emiel/285178bb423d04b0f259c42931992d03 to your computer and use it in GitHub Desktop.
Save emiel/285178bb423d04b0f259c42931992d03 to your computer and use it in GitHub Desktop.
Delete multiple key/value pairs or string elements from left operand
--
-- Coming in PostgreSQL 10
-- https://www.postgresql.org/docs/10/static/functions-json.html
--
create or replace function jsonb_delete_array(data jsonb, keys text[])
returns jsonb immutable language sql
as $function$
select coalesce((
select jsonb_object_agg(key, value)
from jsonb_each(data)
where key <> all(keys)
), '{}'::jsonb)
$function$;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment