Skip to content

Instantly share code, notes, and snippets.

@jenikm
Created November 18, 2015 04:41
Show Gist options
  • Save jenikm/09774a633494cd03c989 to your computer and use it in GitHub Desktop.
Save jenikm/09774a633494cd03c989 to your computer and use it in GitHub Desktop.
Removes key from json object within postgres
CREATE OR REPLACE FUNCTION "json_object_delete_keys"("json" json, VARIADIC "keys_to_delete" TEXT[])
RETURNS json
LANGUAGE sql
IMMUTABLE
STRICT
AS $function$
SELECT COALESCE(
(SELECT ('{' || string_agg(to_json("key") || ':' || "value", ',') || '}')
FROM json_each("json")
WHERE "key" <> ALL ("keys_to_delete")),
'{}'
)::json
$function$;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment