Skip to content

Instantly share code, notes, and snippets.

@chapmanjacobd
Last active September 1, 2020 06:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chapmanjacobd/983c28c04c5ee35d1b21eeffabf1c40d to your computer and use it in GitHub Desktop.
Save chapmanjacobd/983c28c04c5ee35d1b21eeffabf1c40d to your computer and use it in GitHub Desktop.
do
$$
declare
keys_u text;
begin
drop view if exists v_flat_u cascade;
select
string_agg(distinct format('data_json ->> %L as %I',rc, rc), ', ') data_json
into keys_u
from "dev",
jsonb_array_elements(users) ra, jsonb_strip_nulls(ra) rb, jsonb_object_keys(rb) as r(rc);
execute 'create view v_flat_u as select id, '||keys_u||' from "dev", jsonb_array_elements(users) as data_json';
end;
$$
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment