Tutorial: Loading JSON data into a relational table (2024)

Introduction

When uploading JSON data into a table, you have these options:

The COPY command in this tutorial uses a SELECT statement to query for individual elements in a staged JSON file.

The example commands provided in this tutorial includes a PUT statement.We recommend executing these commands in SnowSQL which supports the PUT command.Clients such as Snowsight and the Classic Console donot support the PUT command.

Prerequisites

For this tutorial you need to:

  • Download a Snowflake provided JSON data file.

  • Create a database, a table, and a virtual warehouse for this tutorial.

Database, table, and virtual warehouse are basic Snowflake objects required formost Snowflake activities.

Data file for loading

To download the sample JSON data file, click sales.json.If clicking the link does not download the file, right-click the link and save thelink/file to your local file system.

The tutorial assumes you unpacked the JSON data file in to the following directories:

  • Linux/macOS: /tmp/load

  • Windows: C:\tempload

The data file include sample home sales JSON data. An example JSON object is shown:

{ "location": { "state_city": "MA-Lexington", "zip": "40503" }, "sale_date": "2017-3-5", "price": "275836"}

Copy

Creating the database, table, and virtual warehouse

The following commands create objects specifically for use with this tutorial.When you have completed the tutorial, you can drop the objects.

 create or replace database mydatabase; use schema mydatabase.public;CREATE OR REPLACE TEMPORARY TABLE home_sales ( city STRING, zip STRING, state STRING, type STRING DEFAULT 'Residential', sale_date timestamp_ntz, price STRING );create or replace warehouse mywarehouse with warehouse_size='X-SMALL' auto_suspend = 120 auto_resume = true initially_suspended=true;use warehouse mywarehouse;

Copy

Note these commands creates temporary table. Temporary tables persist only forthe duration of the user session and is not visible to other users.

Create file format object

Execute the CREATE FILE FORMAT commandto create the sf_tut_json_format file format.

CREATE OR REPLACE FILE FORMAT sf_tut_json_format TYPE = JSON;

Copy

TYPE = 'JSON' indicates the source file format type. CSV is the default file format type.

Create stage object

Execute CREATE STAGE to create theinternal sf_tut_stage stage.

CREATE OR REPLACE TEMPORARY STAGE sf_tut_stage FILE_FORMAT = sf_tut_json_format;

Copy

Similar to temporary tables, temporary stages are automatically droppedat the end of the session.

Stage the data file

Execute the PUT command to upload the JSON file from your local file system to thenamed stage.

Copy data into the target table

Load the sales.json.gz staged data file into the home_sales table.

COPY INTO home_sales(city, state, zip, sale_date, price) FROM (SELECT SUBSTR($1:location.state_city,4), SUBSTR($1:location.state_city,1,2), $1:location.zip, to_timestamp_ntz($1:sale_date), $1:price FROM @sf_tut_stage/sales.json.gz t) ON_ERROR = 'continue';

Copy

Note the $1 in the SELECT query refers to the single column where the JSON is stored.The query also uses the following functions:

Execute the following query to verify data is copied.

SELECT * from home_sales;

Copy

Remove the successfully copied data files

After you verify that you successfully copied data from your stage into the tables,you can remove data files from the internal stage using the REMOVEcommand to save on data storage.

REMOVE @sf_tut_stage/sales.json.gz;

Copy

Clean up

Execute the following DROP <object> commands to return your system to its state before you began the tutorial:

DROP DATABASE IF EXISTS mydatabase;DROP WAREHOUSE IF EXISTS mywarehouse;

Copy

Dropping the database automatically removes all child database objects such as tables.

Tutorial: Loading JSON data into a relational table (2024)
Top Articles
Ether could surge 500% this cycle as new ETFs trigger a massive bull run, crypto expert says
This Is the Average Social Security Payment in Every State
Aberration Surface Entrances
Login Page
Chatiw.ib
Identifont Upload
Guardians Of The Galaxy Showtimes Near Athol Cinemas 8
Dr Doe's Chemistry Quiz Answer Key
CA Kapil 🇦🇪 Talreja Dubai on LinkedIn: #businessethics #audit #pwc #evergrande #talrejaandtalreja #businesssetup…
Mylife Cvs Login
Strange World Showtimes Near Cmx Downtown At The Gardens 16
Osrs Blessed Axe
Assets | HIVO Support
Chris Hipkins Fue Juramentado Como El Nuevo Primer Ministro De...
Hood County Buy Sell And Trade
Ou Class Nav
Gdp E124
Swedestats
Craigslist West Valley
V-Pay: Sicherheit, Kosten und Alternativen - BankingGeek
Curry Ford Accident Today
Decosmo Industrial Auctions
Yisd Home Access Center
Contracts for May 28, 2020
Encyclopaedia Metallum - WikiMili, The Best Wikipedia Reader
Restored Republic June 16 2023
Cb2 South Coast Plaza
The 15 Best Sites to Watch Movies for Free (Legally!)
From This Corner - Chief Glen Brock: A Shawnee Thinker
Mta Bus Forums
Craigslist Auburn Al
Craigs List Jax Fl
Southtown 101 Menu
Dailymotion
Greater Orangeburg
County Cricket Championship, day one - scores, radio commentary & live text
Pfcu Chestnut Street
Loopnet Properties For Sale
Smayperu
Etowah County Sheriff Dept
Empire Visionworks The Crossings Clifton Park Photos
Pain Out Maxx Kratom
R: Getting Help with R
2017 Ford F550 Rear Axle Nut Torque Spec
Gw2 Support Specter
Adams-Buggs Funeral Services Obituaries
Sleep Outfitters Springhurst
Samantha Lyne Wikipedia
Congressional hopeful Aisha Mills sees district as an economical model
Cognitive Function Test Potomac Falls
Dr Seuss Star Bellied Sneetches Pdf
Latest Posts
Article information

Author: Twana Towne Ret

Last Updated:

Views: 5825

Rating: 4.3 / 5 (44 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Twana Towne Ret

Birthday: 1994-03-19

Address: Apt. 990 97439 Corwin Motorway, Port Eliseoburgh, NM 99144-2618

Phone: +5958753152963

Job: National Specialist

Hobby: Kayaking, Photography, Skydiving, Embroidery, Leather crafting, Orienteering, Cooking

Introduction: My name is Twana Towne Ret, I am a famous, talented, joyous, perfect, powerful, inquisitive, lovely person who loves writing and wants to share my knowledge and understanding with you.