We have a JSON file in s3, with base64 encoded JSON events. The decoded events will be of format
{
"payload": <bytes>,
"context": {
"metadata1": "metadata1_val",
"metadata2": "metadata2_val"
}
}
where payload is base64 encoded JSON as well. Decoded payload will be of format
{
"event_key1": "event_val1",
"event_key2": "event_val2",
"event_key3": "event_val3",
}
Since Snowflake stage file formats are restricted to CSV, JSON, AVRO, ORC, PARQUET, XML, without any direct support for binary format, we created a custom JSON file format as follows
create or replace file format BINARY_JSON_FORMAT
type = 'JSON'
BINARY_FORMAT = 'BASE64'
strip_outer_array = true;
Couldn't get this to load binary data into a single column to be decoded accordingly.
CREATE OR REPLACE STAGE TEST_STAGE
storage_integration = <storage_integration>
url='s3://bukit/test/'
file_format = BINARY_JSON_FORMAT;
// The following could not parse the file
select parse_json(base64_decode_string($1)):payload as payload from @TEST_STAGE/test-file;
Any thoughts on how this could be achieved? We want to wrap this select in a copy into statement to create a snowpipe to ingest data from s3.
COPY INTO TEST_TABLE (
event_key1,
event_key2,
event_key3
) from (
select
parse_json(base64_decode_string($1)):payload.event_key1 event_key1,
parse_json(base64_decode_string($1)):payload.event_key2 event_key2,
parse_json(base64_decode_string($1)):payload.event_key3 event_key3,
from @TEST_STAGE
);