Advertisement
kromm77

Untitled

Mar 21st, 2016
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.55 KB | None | 0 0
  1. --http://stackoverflow.com/questions/18209625/how-do-i-modify-fields-inside-the-new-postgresql-json-datatype
  2.  
  3. CREATE OR REPLACE FUNCTION "json_object_set_key"(
  4. "json" json,
  5. "key_to_set" TEXT,
  6. "value_to_set" anyelement
  7. )
  8. RETURNS json
  9. LANGUAGE sql
  10. IMMUTABLE
  11. STRICT
  12. AS $function$
  13. SELECT concat('{', string_agg(to_json("key") || ':' || "value", ','), '}')::json
  14. FROM (SELECT *
  15. FROM json_each("json")
  16. WHERE "key" <> "key_to_set"
  17. UNION ALL
  18. SELECT "key_to_set", to_json("value_to_set")) AS "fields"
  19. $function$;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement