sqlsnowflake-cloud-data-platform

Snowflake: how to form a string from an ARRAY with OBJECT types inside?


I have a column TAGS of type ARRAY in table ITEM that looks like this:

TAGS
[{"Name": 'Alice', "Department": 'Sales', "ID": 123}, {"Name": 'Bob', "Department": 'HR', "ID": 456}]
[{"Name": 'Charles', "Department": 'Director', "ID": 789}]

As you can see, the number of elements inside the ARRAY does not match. What I want is to construct a string for each row, that gets only the Name and ID of all elements, separated by the character ,. This is the desired result:

TAGS
'Alice: 123, Bob: 456'
'Charles: 789'

How can I do that in Snowflake? Ideally, I would like to use it in a SELECT statement.


Solution

  • I am able to figure this out. The trick is to use a combination of LATERAL FLATTEN and LISTAGG. A requirement is the table must have an ID associated with a row (which can be easily generated):

    ID TAGS
    1 [{"Name": 'Alice', "Department": 'Sales', "ID": 123}, {"Name": 'Bob', "Department": 'HR', "ID": 456}]
    2 [{"Name": 'Charles', "Department": 'Director', "ID": 789}]

    Then we can use:

    select list_agg(value:"Name"::varchar || ': ' || value:"ID"::varchar, ', ')
    from ITEM,
    lateral flatten(input => ITEM.TAGS)
    group by ID;