snowflake-cloud-data-platform

Create column type of ARRAY<JSON> in Snowflake


Im trying to move from BQ to Snowflake but I have a problem creating my table schema. I have a in BQ columns defined as:

ARRAY<STRUCT<name STRING, id STRING>>

But I didnt find how to do the same in Snowflake. I have read both docs: https://cloud.google.com/bigquery/docs/migration/snowflake-sql

https://docs.snowflake.com/en/sql-reference/data-types-structured#label-structured-types-specifying-map

And I dont understand how to do it.


Solution

  • VARIANT holds both JSON and ARRAY's thus, an ARRAY of JSON is still VARIANT.

    What it does not do is type/shape inforcement. It is just an anything type.

    So some data turned into VARIANT via PARSE_JSON:

    select 
        $1 as str,
        PARSE_JSON(str) as json,
        TYPEOF(json) as type,
        SYSTEM$TYPEOF(json) as sys_type
    from values
        ('["arrray","of","string"]'), /* array of string */
        ('{"name": "foo", "id":1}'), /* plan JS Object */
        ('[{"name": "foo", "id":1}, {"name": "bar", "id":2}]') /* array of plan JS Object */
    ;
    

    enter image description here

    Here you can see Snowflake know if the object is an ARRAY or OBJECT at the top level, but the column type is VARIANT.

    So if you want to have a table that has your array of object, you would use