Skip to content

Instantly share code, notes, and snippets.

View Aitem's full-sized avatar
🏠
Working from home

Marat Surmashev Aitem

🏠
Working from home
View GitHub Profile
@Aitem
Aitem / jsonb_helper.sql
Last active December 16, 2022 18:46
PostreSQL JSONB helpers functions
-- jsonb_select_keys take jsonb and keys and return jsonb
-- contains only given keys
create or REPLACE function jsonb_select_keys (resource jsonb, keys text[] ) returns jsonb
as $$
select jsonb_object_agg(k, v)
from
(select unnest(keys) k ) k ,
lateral (select resource->k.k as v) t;
$$ LANGUAGE SQL
IMMUTABLE;