Advertisement
kromm77

postgresql json replace attribute

Mar 21st, 2016
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. CREATE OR REPLACE FUNCTION "json_object_set_key"(
  2.   "json"          json,
  3.   "key_to_set"    TEXT,
  4.   "value_to_set"  anyelement
  5. )
  6.   RETURNS json
  7.   LANGUAGE sql
  8.   IMMUTABLE
  9.   STRICT
  10. AS $function$
  11. SELECT concat('{', string_agg(to_json("key") || ':' || "value", ','), '}')::json
  12.   FROM (SELECT *
  13.           FROM json_each("json")
  14.          WHERE "key" <> "key_to_set"
  15.          UNION ALL
  16.         SELECT "key_to_set", to_json("value_to_set")) AS "fields"
  17. $function$;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement