-
-
Save jngbng/f6894f744f08e79ab82c91d2664aa576 to your computer and use it in GitHub Desktop.
PostgreSQL: Remove Multiple Keys From JSONB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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