Get started with Secoda
See why hundreds of industry leaders trust Secoda to unlock their data's full potential.
See why hundreds of industry leaders trust Secoda to unlock their data's full potential.
The FLATTEN function in Snowflake is designed to transform semi-structured data into a relational format. It is particularly useful when dealing with VARIANT, OBJECT, or ARRAY columns, converting hierarchical data structures into multiple rows for easier querying and analysis.
FLATTEN (
INPUT => <expr> [ , PATH => <constant_expr> ] [ , OUTER => TRUE | FALSE ] [ , RECURSIVE => TRUE | FALSE ] [ , MODE => 'OBJECT' | 'ARRAY' | 'BOTH' ]
)
This code snippet shows the syntax for the FLATTEN function. The INPUT parameter is required and must be of type VARIANT, OBJECT, or ARRAY. Optional parameters include PATH, OUTER, RECURSIVE, and MODE, which provide additional control over the flattening process.
The FLATTEN function can be used in various scenarios depending on the structure of the semi-structured data and the desired output. Below are some examples demonstrating its use.
SELECT * FROM TABLE (FLATTEN(input => parse_json('[1, 2, 77]'))) f;
The output will show each element of the array as a separate row. SELECT * FROM TABLE (FLATTEN(input => parse_json('{"a":1, "b":[77,88]}'), path => 'b')) f;
The output will show the elements of the array located at the specified path. SELECT * FROM TABLE (FLATTEN(input => parse_json('[]'), outer => true)) f;
The output will generate one row with NULL values for zero-row expansions. SELECT * FROM TABLE (FLATTEN(input => parse_json('{"a":1, "b":[77,88], "c": {"d":"X"}}'))) f;
The output will show all sub-elements of the hierarchical structure. This step demonstrates a basic use of the FLATTEN function to transform an array into multiple rows.
SELECT * FROM TABLE (FLATTEN(input => parse_json('[1, 2, 77]'))) f;
The output will show each element of the array as a separate row, making it easier to query and analyze.
This step shows how to use the PATH parameter to specify a path within the VARIANT data to be flattened.
SELECT * FROM TABLE (FLATTEN(input => parse_json('{"a":1, "b":[77,88]}'), path => 'b')) f;
The output will show the elements of the array located at the specified path, allowing for more targeted data extraction.
This step demonstrates the use of the OUTER parameter to include rows that cannot be expanded.
SELECT * FROM TABLE (FLATTEN(input => parse_json('[]'), outer => true)) f;
The output will generate one row with NULL values for zero-row expansions, ensuring that no data is lost.
This step shows how to use the RECURSIVE parameter to expand all sub-elements recursively.
SELECT * FROM TABLE (FLATTEN(input => parse_json('{"a":1, "b":[77,88], "c": {"d":"X"}}'))) f;
The output will show all sub-elements of the hierarchical structure, providing a comprehensive view of the data.
Some common challenges or errors you might encounter using FLATTEN include: