donderdag 3 december 2026

Databricks series : Understanding schema flexibility with STRUCT, ARRAY, MAP and VARIANT

Introduction

When working with JSON data in Databricks, you will often encounter complex data types such as STRUCT, ARRAY, MAP and VARIANT.  At first glance, these types may look quite similar. They all allow you to work with nested or semi-structured data.  However, there is an important difference: how flexible is the structure of your data?


For example:

  • Can a JSON record contain a field that another record does not have?
  • Can an ARRAY contain a different number of elements?
  • Can a MAP contain different keys for each record?
  • Can every record have a completely different structure?

In this article, we will explore these questions and explain how STRUCT, ARRAY, MAP and VARIANT handle differences in data structures.


We are building upon what we already have :


STRUCT : A fixed structure with optional values

A STRUCT represents an object with a predefined set of named fields. For example, imagine that we define the following STRUCT:


STRUCT<
    customer_id: INT,
    name: STRING,
    country: STRING,
    email: STRING
>


We have defined four fields:

  • customer_id
  • name
  • country
  • email

Does this mean that every customer must have a value for all four fields? No. A field can contain NULL.



Sarah does not have an email address. That is perfectly valid.  The important distinction is that the email field is still part of the STRUCT definition. Its value is simply NULL. Key takeaway: STRUCT has a fixed schema, but individual field values can be NULL.


ARRAY : A collection with a consistent element type

An ARRAY represents a collection of elements. An important rule is that all elements in an ARRAY must have the same data type.  For example:


ARRAY<STRING>


The following values are valid:

["Dutch", "English", "French"]


An ARRAY can also contain NULL elements:

["Dutch", null, "French"]


The number of elements does not have to be the same in every record.  For example, one customer could have three languages while another customer has only one.


ARRAY containing STRUCTs

An ARRAY can also contain more complex elements, such as STRUCTs.


For example:

ARRAY<
    STRUCT<
        order_id: INT,
        amount: DECIMAL(10,2)
    >
>


This represents a list of orders.  Each order has the same structure:

  • order_id
  • amount

You can have different numbers of orders, but every order follows the same STRUCT definition. The amount field can still contain NULL if it is nullable. Key takeaway: ARRAY allows a variable number of elements, but the element type remains consistent.


MAP : Different keys for different records

A MAP is a collection of key-value pairs.  For example:

MAP<STRING, STRING>


This means: Keys are STRING values.  alues are STRING values.  But here is an important difference compared with STRUCT.  The actual keys do not have to be the same in every record.


Consider these two customer records.


Customer 1

{
    "language": "Dutch",
    "currency": "EUR"
}


Customer 2

{
    "language": "English",
    "theme": "dark"
}


Both records are valid MAP values. Customer 1 has a currency key, while customer 2 has a theme key. The MAP definition remains the same because both records use STRING keys and STRING values. If you try to retrieve a key that does not exist, the result is NULL.


For example:

SELECT
    element_at(preferences, 'currency') AS currency
FROM main.demo.customer_preferences;


For customer 2, this returns NULL because the currency key is not present.

Key takeaway: MAP has fixed key and value data types, but the actual keys can differ between records.


VARIANT : A completely flexible structure

VARIANT takes flexibility one step further. Unlike STRUCT, ARRAY and MAP, VARIANT does not require you to define the complete structure in advance. Each record can contain different fields, nested objects, arrays and values. Consider these three JSON records.


Record 1

{
    "customer_id": 1001,
    "name": "Jan",
    "country": "Netherlands"
}


Record 2

{
    "customer_id": 1002,
    "name": "Sarah",
    "preferences": {
        "language": "English"
    },
    "orders": [5001, 5002]
}


Record 3

{
    "customer_id": 1003,
    "name": "Peter",
    "email": "peter@example.com",
    "active": true
}


Notice that the three records have different structures.

  • Record 1 contains a country.

  • Record 2 contains preferences and an array of orders.

  • Record 3 contains an email address and an active flag.

All three records can be stored in the same VARIANT column.


For example:

CREATE OR REPLACE TABLE main.demo.customer_variant
(
    payload VARIANT
);


You can insert the JSON documents using PARSE_JSON():

INSERT INTO main.demo.customer_variant
VALUES
(
    PARSE_JSON(
        '{"customer_id":1001,"name":"Jan","country":"Netherlands"}'
    )
),
(
    PARSE_JSON(
        '{"customer_id":1002,"name":"Sarah","preferences":{"language":"English"},"orders":[5001,5002]}'
    )
),
(
    PARSE_JSON(
        '{"customer_id":1003,"name":"Peter","email":"peter@example.com","active":true}'
    )
);


You can then extract fields using the VARIANT path syntax:

SELECT
    payload:customer_id::INT AS customer_id,
    payload:name::STRING AS customer_name,
    payload:country::STRING AS country,
    payload:email::STRING AS email
FROM main.demo.customer_variant;


Fields that are not present in a particular record return NULL.  This makes VARIANT especially useful when ingesting data from external APIs or event streams where the JSON structure can change over time.


Key takeaway: VARIANT allows each record to have a different structure within the same column.


5. Comparing the four data types

Let's summarize the differences.




There is one important detail:

  • STRUCT has fixed field names, but values can be NULL.

  • ARRAY has a fixed element type, but the number of elements can vary.

  • MAP has fixed key and value types, but the actual keys can vary.

  • VARIANT allows the entire JSON structure to vary.


6. When should you use which data type?

A simple rule of thumb:


Use STRUCT when:

  • You know the fields in advance.

  • You want a clearly defined schema.

  • You are working with objects such as customer addresses or product details.


Use ARRAY when:

  • You have a list of repeating elements.

  • You want to store multiple values of the same type.

  • You are working with orders, transactions or event collections.


Use MAP when:

  • You have dynamic attributes.

  • The keys can differ between records.

  • You want to store flexible key-value pairs.


Use VARIANT when:

  • You do not know the complete JSON structure in advance.

  • The structure can change between records.

  • You are ingesting data from external APIs or event streams.


Conclusion

STRUCT, ARRAY, MAP and VARIANT all help us work with complex data in Databricks, but they offer different levels of flexibility. The main difference is not whether a value can be NULL. Even a STRUCT can contain NULL values. The real difference is how much of the data structure must be defined in advance. STRUCT, ARRAY and MAP have defined type structures, while VARIANT allows the complete structure to vary between records.This is particularly useful when designing a data ingestion process. You can initially store flexible JSON payloads using VARIANT and then extract and validate the fields you need in your Prep layer.


In other words:

Use VARIANT when you need flexibility at ingestion time, and use STRUCT, ARRAY and MAP when you want more explicitly defined data structures.


Geen opmerkingen:

Een reactie posten