Skip to content

Instantly share code, notes, and snippets.

@jngbng
Forked from yyscamper/jsonb_remove_keys.sql
Created February 20, 2019 03:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jngbng/f6894f744f08e79ab82c91d2664aa576 to your computer and use it in GitHub Desktop.
Save jngbng/f6894f744f08e79ab82c91d2664aa576 to your computer and use it in GitHub Desktop.
PostgreSQL: Remove Multiple Keys From JSONB
CREATE OR REPLACE FUNCTION jsonb_remove_keys(
jdata JSONB,
keys TEXT[]
)
RETURNS JSONB AS $$
DECLARE
result JSONB;
len INT;
target TEXT;
BEGIN
len = array_length(keys, 1);
result = jdata;
FOR i IN 1..len LOOP
target = keys[i];
IF (jdata ? target) THEN
result = (result - target);
END IF;
END LOOP;
RETURN result;
END;
$$ LANGUAGE plpgsql;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment