Typed OBJECT, ARRAY and MAP
Structured types declare the types inside a collection. They make nested contracts explicit.
Understand the idea
The standalone query casts flexible values into declared structures. Incompatible values can fail. TRY_CAST is not supported for structured types, so validate inputs before conversion.
| Concept | What it means |
|---|---|
| OBJECT(city VARCHAR) | Known field names with declared types. |
| ARRAY(NUMBER) | A sequence of numeric elements. |
| MAP(VARCHAR, NUMBER) | Variable string keys with numeric values. |
| Semi-structured OBJECT / ARRAY | Flexible nested values without the same declared element contract. |
A concrete example
A fixed address shape fits a typed OBJECT; a changing set of SKU quantities fits a MAP.
Remember: Choose flexibility for raw capture and explicit types where consumers need a stable contract.
Explain it step by step · full theory
How to apply it
The standalone query casts flexible values into declared structures. Incompatible values can fail. TRY_CAST is not supported for structured types, so validate inputs before conversion.
Typed objects use a declared field set; maps allow variable keys with a shared value type. These examples target standard Snowflake tables or SELECT expressions. Follow Module 06 for schema inference/evolution instead of treating a cast as automatic evolution.
Words used in this lesson
- OBJECT(city VARCHAR)
- Known field names with declared types.
- ARRAY(NUMBER)
- A sequence of numeric elements.
- MAP(VARCHAR, NUMBER)
- Variable string keys with numeric values.
- Semi-structured OBJECT / ARRAY
- Flexible nested values without the same declared element contract.
See how it works
Choose a step to explore the idea at your own pace.
Open the interactive explanation
Typed OBJECT, ARRAY and MAP
Read the shape
Structured types declare the types inside a collection. They make nested contracts explicit.
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
Structured types declare the types inside a collection. They make nested contracts explicit.
- Follow the example
A fixed address shape fits a typed OBJECT; a changing set of SKU quantities fits a MAP.
- Verify the result
Choose flexibility for raw capture and explicit types where consumers need a stable contract.
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 three typed collections
WITH typed AS (
SELECT PARSE_JSON('{"city":"Pune"}')::OBJECT(city VARCHAR) AS ADDRESS,
PARSE_JSON('[2,1]')::ARRAY(NUMBER) AS QUANTITIES,
PARSE_JSON('{"A":2,"B":1}')::MAP(VARCHAR, NUMBER) AS STOCK
)
SELECT GET(ADDRESS, 'city') AS CITY,
GET(QUANTITIES, 0) AS FIRST_QTY,
GET(STOCK, 'A') AS A_STOCK FROM typed;What to expect and why
Returns Pune, 2 and 2. The declared child types are part of the contract.
Declare nested columns on a standard table
CREATE TABLE M7_TYPED (
EVENT_ID NUMBER,
ADDRESS OBJECT(city VARCHAR),
QUANTITIES ARRAY(NUMBER),
STOCK MAP(VARCHAR, NUMBER)
);What to expect and why
Requires the lab setup. This creates the typed schema without loading rows.
Check your understanding
Choose an answer, then check your reasoning. This is practice, not a scored test.
Try it yourself
Where would you use raw VARIANT and where would you use typed columns?
Compare your reasoning
Preserve incoming documents in a raw layer; expose validated fields and typed collections in consumer models when their contracts are stable.