Skip to content

Instantly share code, notes, and snippets.

View pyrliu's full-sized avatar

Pyry Julius Liukas pyrliu

  • Taiste
  • Finland
View GitHub Profile
@villelahdenvuo
villelahdenvuo / jsonb_set_deep.sql
Created June 14, 2016 12:24
PostgreSQL jsonb_set is nice, but it does't create missing objects so I made this little helper.
CREATE OR REPLACE FUNCTION jsonb_set_deep(target jsonb, path text[], val jsonb)
RETURNS jsonb AS $$
DECLARE
k text;
p text[];
BEGIN
-- Create missing objects in the path.
FOREACH k IN ARRAY path LOOP
p := p || k;
IF (target #> p IS NULL) THEN