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.
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.
Use the window function
array_agg
to reduce a column to an array.The query above produces the following:
You can then reduce that to the unique
last_name
,first_name
pairs using thedistinct
operator.To convert the array of values returned by
array_agg
to JSON, you can simply cast the result using::variant
.