Search code examples
jsonsnowflake-cloud-data-platformdbt

Create JSON from a subquery in snowflake


I want to create a JSON string from a list of value, but I've never worked with JSON before. Please see the image below for my 2 tables, and what I want to create on the right.

enter image description here

I tried this, but it doesn't work (excuse my naivety...but I thought this would be the logical implementation of it)

select a.property_key
      ,to_JSON( select application_ID from tableB where a.property_key = b.property_key) as application_list
  from tableA a

I would appreciate the help. I tried googleing, but I find the snowflake documentation very confusing.


Solution

  • Use the window function array_agg to reduce a column to an array.

    create table names (
      last_name varchar,
      first_name varchar
    );
    
    insert  into names 
        (last_name, first_name) 
    values 
        ('waterman', 'tom'), 
        ('waterman', 'alex'),
        ('waterman', 'david'),
        ('barnett', 'rebecca'),
        ('barnett', 'anne');
        
    select
        last_name,
        array_agg(first_name) over (partition by last_name) first_names
    from names;
    

    The query above produces the following:

    LAST_NAME   FIRST_NAMES
    waterman    ["tom", "alex", "david" ]
    waterman    ["tom", "alex", "david" ]
    waterman    ["tom", "alex", "david" ]
    barnett     ["rebecca", "anne"  ]
    barnett     ["rebecca", "anne"  ]
    

    You can then reduce that to the unique last_name, first_name pairs using the distinct operator.

    select
        distinct 
          last_name,
          array_agg(first_name) over (partition by last_name) first_names
    from names;
    

    To convert the array of values returned by array_agg to JSON, you can simply cast the result using ::variant.

    select
        distinct 
          last_name,
          array_agg(first_name) over (partition by last_name)::variant first_names
    from names;