VARIANT, OBJECT and ARRAY: read the shape first
An OBJECT holds named values, an ARRAY holds ordered values, and VARIANT can hold either shape or a scalar.
Understand the idea
This module uses a dedicated M7_LAB schema in the existing Module 03 database. Keep it separate from M6_LAB. Most early examples are standalone SELECT statements; the first setup creates objects for file loading and the final lab.
| Concept | What it means |
|---|---|
| OBJECT | A set of key/value pairs, such as customer.name. |
| ARRAY | A sequence, such as the items in an order. |
| VARIANT | A flexible value whose stored type can vary. |
| PARSE_JSON | Turns JSON text into a queryable value. |
A concrete example
A purchase event has one customer object and an items array. Each item is another object.
Remember: JSON-looking text is still text until it is parsed. Inspect the stored type before querying it.
Explain it step by step · full theory
How to apply it
This module uses a dedicated M7_LAB schema in the existing Module 03 database. Keep it separate from M6_LAB. Most early examples are standalone SELECT statements; the first setup creates objects for file loading and the final lab.
Use an authorized warehouse and role. Setup needs database USAGE and CREATE SCHEMA, then CREATE TABLE, STAGE and FILE FORMAT. A separate loader needs database/schema/warehouse USAGE, stage READ, file-format USAGE and target INSERT; inspection needs SELECT. Use fresh object names when repeating setup.
Words used in this lesson
- OBJECT
- A set of key/value pairs, such as customer.name.
- ARRAY
- A sequence, such as the items in an order.
- VARIANT
- A flexible value whose stored type can vary.
- PARSE_JSON
- Turns JSON text into a queryable value.
See how it works
Choose a step to explore the idea at your own pace.
Open the interactive explanation
VARIANT, OBJECT and ARRAY: read the shape first
Read the shape
An OBJECT holds named values, an ARRAY holds ordered values, and VARIANT can hold either shape or a scalar.
Automatic playback is off for reduced motion. Select any step or use Previous and Next. Illustrations explain behavior; they are not live measurements.
Read the complete diagram explanation
- Read the shape
An OBJECT holds named values, an ARRAY holds ordered values, and VARIANT can hold either shape or a scalar.
- Follow the example
A purchase event has one customer object and an items array. Each item is another object.
- Verify the result
JSON-looking text is still text until it is parsed. Inspect the stored type before querying it.
Try the SQL or command
Read → predict → run in your learning environment → compare. You can also learn by reading without an account.
New to SQL? Where to run it and how to read it
A query is a question written in SQL. Snowflake runs it and returns a result table. This website shows examples; it does not connect to your account.
- No account yet? Read and predict the output first. Follow the account and Snowsight lesson when you are ready.
- For SQL practice, use a learning account in Snowsight and select an authorized role and warehouse. A role determines permissions; a warehouse supplies processing power. Follow the example’s database/schema setup when it refers to stored objects.
- Read the entire example, including comments beginning with
--. Run the selected statement or labeled block in the stated order. Examples labeled Python, shell or HTTP belong in that client, not in a SQL editor. - Compare values and row counts with the explained result. If an example deliberately causes an error, follow its rollback and cleanup steps before continuing.
- SELECT / FROM
- Choose the values to return / identify their input.
- VALUES / COLUMN1
- VALUES supplies tiny example rows. COLUMN1 means their first column, COLUMN2 their second.
- AS / WITH
- AS gives a result a readable name. WITH names a temporary query step within one statement.
- NULL / ::
- NULL means missing or unknown. The :: notation converts a value to a named data type.
- Session
- One connection and its settings. Keep related transaction steps in the same session.
- Database.schema.table
- A full address for stored data. It is separate from the warehouse that processes queries.
Need the L2_OBJECTS_LAB setup? Open the first database-object lesson →
Help with common practice errors
- No active warehouse: select a warehouse your role can use. Do not assume you need to create a new one.
- Object missing or not authorized: check the full name, required setup and active role. Ask the account owner for access if needed.
- Object already exists: setup may already have run. Read the current state or choose a fresh lab name consistently; do not add OR REPLACE blindly.
- Different result: check whether an earlier INSERT or UPDATE changed the data. VALUES examples provide their own fresh input for each statement.
Before running this lesson
Manual learning examples; nothing runs on this website. Standalone WITH/SELECT examples supply their own input. Table and file exercises require the first lesson’s L2_OBJECTS_LAB.M7_LAB setup and an authorized warehouse. Select that schema in every session. The final lab requires the supplied events.json to be loaded first.
Compare parsing with wrapping text
SELECT TYPEOF(PARSE_JSON('{"qty":2}')) AS PARSED_TYPE,
TYPEOF(TO_VARIANT('{"qty":2}')) AS WRAPPED_TYPE,
TYPEOF(PARSE_JSON('["A","B"]')) AS LIST_TYPE;What to expect and why
Returns OBJECT, VARCHAR and ARRAY. TO_VARIANT wraps the string; it does not interpret its JSON structure.
One-time setup for the shared lab
USE DATABASE L2_OBJECTS_LAB;
CREATE SCHEMA M7_LAB;
USE SCHEMA M7_LAB;
CREATE STAGE M7_FILES;
CREATE FILE FORMAT M7_JSON TYPE=JSON;
CREATE TABLE M7_EVENTS_RAW (PAYLOAD VARIANT);What to expect and why
Creates empty lab objects. Select your authorized warehouse. In new sessions use USE SCHEMA L2_OBJECTS_LAB.M7_LAB before continuing.
Check your understanding
Choose an answer, then check your reasoning. This is practice, not a scored test.
Try it yourself
Choose a shape for one customer and their several phone numbers.
Compare your reasoning
Use a customer object containing an array of phone numbers. Keep identifiers and expected element types explicit.