简体   繁体   中英

Alternative to JSON Flattening via Target Table in Snowflake

Per snowflake: https://docs.snowflake.net/manuals/user-guide/json-basics-tutorial-copy-into.html I created a target table (Testing_JSON), that is a single Variant column that contains an uploaded JSON file.

My Question is How can I cut out creating this "Target Table (ie Testing_JSON)" that is a single Variant Column that I have to reference to create the actual and only table I want (TABLE1), which contains the flattened JSON. I have found no way to read in a JSON file from my desktop and 'parse it on the fly' to create a flattened table, VIA THE UI. Not using the CLI as I know this can be done using PUT/COPY INTO

create or replace temporary table TABLE1 AS
SELECT 
VALUE:col1::string AS COL_1,
VALUE:col2::string AS COL_2,
VALUE:col3::string AS COL_3

from TESTING_JSON 
  , lateral flatten( input => json:value);

You're going to need to do this in a few steps from your desktop.

  1. use SnowSQL or some other tool to get your JSON file up to blob storage: https://docs.snowflake.net/manuals/sql-reference/sql/put.html
  2. use a COPY INTO statement to get the data loaded directly to the flattened table that you want to load to. This will require a SELECT statement in your COPY INTO: https://docs.snowflake.net/manuals/sql-reference/sql/copy-into-table.html

There is a good example of this here:

https://docs.snowflake.net/manuals/user-guide/querying-stage.html#example-3-querying-elements-in-a-json-file

You can't do this through the UI. If you want to do this then you need to use an external tool on your desktop or - as Mike mentioned - in the COPY statement.

Here is the link to one of my previous replies to extract the elements.

https://stackoverflow.com/questions/58486204/how-should-i-load-xml-file-which-has-comments-and-spaces-in-them-and-then-using/58514826#58514826

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM