Skip to content

Instantly share code, notes, and snippets.

@ernestoongaro
Last active September 20, 2021 11:44
Show Gist options
  • Save ernestoongaro/e6b5eb2f5a4a618a6f1b911714f12500 to your computer and use it in GitHub Desktop.
Save ernestoongaro/e6b5eb2f5a4a618a6f1b911714f12500 to your computer and use it in GitHub Desktop.
json example

First run this in snowflake:

(
 src variant
)
AS SELECT parse_json(column1) as src
FROM values
('{ 
 "topleveldate" : "2017-04-28", 
 "toplevelname" : "somename", 
 "extraFields": [ 
 { 
 "value": "somevalue1", 
 "key": "somekey1", 
 "type": "sometype1", 
 "booleanflag": false 
 }, 
 { 
 "value": "", 
 "key": "somekey2"
 }]}');

then create a model in dbt called flatten.sql

WITH a as
(
 select 
   src:topleveldate::string as topleveldate
 , src:toplevelname::string as toplevelname
 , value as val
 from vnt, lateral flatten( input => src:extraFields )
)
select topleveldate, toplevelname, key, value from a, lateral flatten( input => val )

then run the model dbt run --models flatten

finally, run select * from {{ ref('flatten') }}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment