Introduction
A MAP is a collection of key-value pairs. It is similar to a Python dictionary. Unlike a STRUCT, a MAP is useful when the available keys may vary between records.
We are building upon what we already have :
- Databricks series : Starting a new python project in Visual Studio Code (Part I)
- Databricks series : setting up a connection to Databricks from local IDE (Part II)
- Databricks series : Setting up a Databricks project in Visual Studio Code (part III)
- Databricks series : Deployment from Visual Studio Code (Part IV)
- Databricks series : Execute the job in Databricks (Part V)
- Databricks series : A configrable framework in Databricks (Part VI)
- Databricks series : Loading strategies in Databricks (Part VII)
- Databricks series : Data contracts in Databricks (Part VIII)
- Databricks series : Debugging Python code in Visual Studio Code with arguments (Part IX)
- Databricks series : ODCS Data contracts in Databricks (Part X)
- Databricks series : reading an API (Part XI)
- Databricks series : Building a mediallion architecture with SCD Type 2 (step 1)(Part XII)
- Databricks series : Building a mediallion architecture with SCD Type 2 (step 2) (Part XIII)
- Databricks series : Building a mediallion architecture with SCD Type 2 (step 3) (Part XIV)
- Databricks series : Working with JSON in Databricks SQL : STRUCT
- Databricks series : Working with JSON in Databricks SQL : ARRAY
Example JSON
Imagine that a customer has several preferences:
{ "customer_id": 1001, "preferences": { "language": "Dutch", "currency": "EUR", "newsletter": "true", "theme": "dark" } }
The preferences property contains dynamic attributes. We can represent this using:
MAP<STRING, STRING>
This means that both the keys and values are strings.
Create a source table
CREATE OR REPLACE TABLE main.demo.customer_map_json ( customer_id BIGINT, payload STRING );
Insert an example record:
INSERT INTO main.demo.customer_map_json VALUES ( 1001, '{ "customer_id": 1001, "preferences": { "language": "Dutch", "currency": "EUR", "newsletter": "true", "theme": "dark" } }' );
Parse the JSON
Use from_json() to convert the preferences object into a MAP.
SELECT from_json( payload, 'STRUCT< customer_id: BIGINT, preferences: MAP<STRING, STRING> >' ) AS customer FROM main.demo.customer_map_json;
Results in
Now Spark understands that preferences is a MAP.
Extract individual values
We can retrieve values using element_at().
SELECT customer.customer_id, element_at( customer.preferences, 'language' ) AS language, element_at( customer.preferences, 'currency' ) AS currency, element_at( customer.preferences, 'newsletter' ) AS newsletter, element_at( customer.preferences, 'theme' ) AS theme FROM ( SELECT from_json( payload, 'STRUCT< customer_id: BIGINT, preferences: MAP<STRING, STRING> >' ) AS customer FROM main.demo.customer_map_json );
Expand all MAP entries
SELECT customer.customer_id, preference_key, preference_value FROM ( SELECT from_json( payload, 'STRUCT< customer_id: BIGINT, preferences: MAP<STRING, STRING> >' ) AS customer FROM main.demo.customer_map_json ) parsed LATERAL VIEW EXPLODE(customer.preferences) exploded AS preference_key, preference_value;
Results in :
This is particularly useful when you want to inspect all available attributes without knowing the keys in advance.
Create a SQL View
For a fixed set of attributes, we can create a regular view:
CREATE OR REPLACE VIEW main.demo.v_customer_preferences AS SELECT customer.customer_id, element_at( customer.preferences, 'language' ) AS language, element_at( customer.preferences, 'currency' ) AS currency, element_at( customer.preferences, 'newsletter' ) AS newsletter, element_at( customer.preferences, 'theme' ) AS theme FROM ( SELECT from_json( payload, 'STRUCT< customer_id: BIGINT, preferences: MAP<STRING, STRING> >' ) AS customer FROM main.demo.customer_map_json );
Query the view:
SELECT * FROM main.demo.v_customer_preferences;
MAP is useful for dynamic attributes where you cannot always predict which keys will be available.
Examples include:
- Customer preferences
- Product attributes
- API metadata
- Event properties
- Dynamic configuration values
If the keys are known and stable, a STRUCT is often a better choice because it provides a clearly defined schema
4. Comparing STRUCT, ARRAY and MAP
We have now seen all three complex data types.
A simple way to remember the difference:
- STRUCT: one object with multiple attributes.
- ARRAY: multiple elements, often repeating objects.
- MAP: dynamically named attributes.
Choosing the right approach for your Databricks architecture
When working with JSON in a Medallion Architecture, I would generally separate the responsibilities.
Raw layer
Keep the original JSON data. This preserves the source information and allows you to reprocess it if the schema changes.
Base layer
Store the ingested data in Delta tables. Depending on the ingestion design, the JSON can remain a STRING or be parsed into complex Spark types.
Prep layer
Parse the JSON using from_json(), validate the structure and extract the attributes needed for downstream processing.
Gold layer
Expose business-friendly SQL Views or tables with clear relational columns.
RAW | | Original JSON v BASE | | Delta tables v PREP | | STRUCT / ARRAY / MAP parsing | Data validation v GOLD | | SQL Views | Business-ready columns v Reporting / Analytics
Conclusion
Databricks SQL provides powerful functionality for working with semi-structured JSON data. With from_json(), we can convert JSON strings into typed structures. We can then use dot notation to access STRUCT fields, EXPLODE() to expand ARRAY elements and ELEMENT_AT() to retrieve values from MAPs. Instead of keeping complex JSON documents in a single column, we can expose the information through reusable SQL Views.
The key is to understand the structure of the JSON first and choose the appropriate Spark data type. Once you understand STRUCT, ARRAY and MAP, working with nested JSON in Databricks becomes much easier.






















.png)
.png)


.png)
.png)
.png)