Created
          November 18, 2015 04:41 
        
      - 
      
- 
        Save jenikm/09774a633494cd03c989 to your computer and use it in GitHub Desktop. 
    Removes key from json object within postgres
  
        
  
    
      This file contains hidden or 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 "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