1

I've found the flatten function but the example is simpler than my real example. It can be created with the following code:

create or replace table test2 as
select '5dd17ef333de385a360e0ef1' as id,
parse_json('{
    "_id" : "5dd17ef333de385a360e0ef1",
    "uid" : "5dd175d3333b85961df27c51",
    "task_info" : {
        "reps=1" : [ 
            [ 
                {
                    "cached" : false,
                    "transform max RAM" : 51445000,
                }
            ], 
            [ 
                {
                    "cached" : false,
                    "transform max RAM" : 51445000,
                }
            ], 
            [ 
                {
                    "cached" : true,
                    "transform max RAM" : 51445000,
                }
            ]
        ]
    }
}')::variant as json_rec
;

Then my attempt to parse it:

select id
, json_rec:_id::string(100) as extracted_id
, value:cached::string as cached
, value
, json_rec
  from
    test2
  , lateral flatten( input => json_rec:task_info )
;

The cached is clearly not going deep enough, although I am unclear of the syntax that is required to fish something like these values out. Thoughts?

2
  • what DBMS are you using? Commented Dec 30, 2019 at 4:22
  • Snowflake is the database Commented Dec 30, 2019 at 4:24

1 Answer 1

3

If what you want is a separate row for each of the items in the innermost array (i.e. 3 rows for the above example), then you can use recursive=>true and filter on key='cached', like this:

select id
, json_rec:_id::string(100) as extracted_id
, value as cached
, json_rec
  from
    test2
  , lateral flatten( input => json_rec:task_info, recursive=>true)
where key='cached';
Sign up to request clarification or add additional context in comments.

4 Comments

my original comment contained an error on my part, solution works, thanks!
follow up - select id , json_rec:_id::string(100) as extracted_id , case when key = 'cached' then value else null end as cached , case when key = 'transform max RAM' then value else null end as transform_max_ram , seq, key, path, index, value, this from test2 , lateral flatten( input => json_rec:task_info, recursive=>true) where key in ('cached', 'transform max RAM') ; how do i indicate the groups of these related values? the only info i have appears to be the first part of the path - but that doesn't seem very elegant...
How do you want the output to look like? 3 rows with a column "cached" and one "transform_max_ram"?
ultimately i turned this into 3 rows for each record and 2 for each field, so 6 total. i then pivot these to columns based on the reps=1... and did a distinct as my data doesn't appear to have a difference for these values.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.