Created
February 26, 2018 09:49
-
-
Save yyscamper/1b8669ea224f3bea826f2100b0cfc108 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
Usage: