zaterdag 28 november 2026

Databricks series : Working with JSON in Databricks SQL : ARRAY

Introduction

An ARRAY represents a collection of elements. A common example is an order list belonging to a customer. One customer can have multiple orders, and each order contains several attributes.


We are building upon what we already have :


An example 


{
  "customer_id": 1001,
  "name": "Jan Jansen",
  "orders": [
    {
      "order_id": "ORD001",
      "amount": 125.50,
      "status": "COMPLETED"
    },
    {
      "order_id": "ORD002",
      "amount": 89.95,
      "status": "OPEN"
    }
  ]
}


The orders property is an ARRAY.

Each element inside the ARRAY is a STRUCT containing:

  • order_id

  • amount

  • status

The resulting Spark data type is conceptually:


Create a source table

Lets create a table :

CREATE OR REPLACE TABLE main.demo.customer_array_json
(
    customer_id BIGINT,
    payload STRING
);

Insert the example data:


INSERT INTO main.demo.customer_array_json
VALUES (
    1001,
    '{
      "customer_id": 1001,
      "name": "Jan Jansen",
      "orders": [
        {
          "order_id": "ORD001",
          "amount": 125.50,
          "status": "COMPLETED"
        },
        {
          "order_id": "ORD002",
          "amount": 89.95,
          "status": "OPEN"
        }
      ]
    }'
);


Parse the JSON

We use from_json() again, but this time we define an ARRAY containing STRUCT elements.


SELECT
    from_json(
        payload,
        'STRUCT<
            customer_id: BIGINT,
            name: STRING,
            orders: ARRAY<
                STRUCT<
                    order_id: STRING,
                    amount: DOUBLE,
                    status: STRING
                >
            >
        >'
    ) AS customer

FROM main.demo.customer_array_json;

Now Spark understands that orders contains multiple order objects. 




Expand the ARRAY using EXPLODE


How do we turn the individual orders into separate rows? For this, we use EXPLODE().

SELECT
    customer.customer_id,
    customer.name,
    order.order_id,
    order.amount,
    order.status

FROM (
    SELECT
        from_json(
            payload,
            'STRUCT<
                customer_id: BIGINT,
                name: STRING,
                orders: ARRAY<
                    STRUCT<
                        order_id: STRING,
                        amount: DOUBLE,
                        status: STRING
                    >
                >
            >'
        ) AS customer

    FROM main.demo.customer_array_json
) parsed

LATERAL VIEW EXPLODE(customer.orders) exploded AS order;

Resulting in : 




Notice what happened: one customer record became two rows because the customer has two orders. This is an important difference compared with STRUCT. A STRUCT extracts fields from an existing row, while EXPLODE expands an ARRAY into multiple rows.


Create a SQL View

We can make this transformation reusable by creating a view.


CREATE OR REPLACE VIEW main.demo.v_customer_orders AS

SELECT
    customer.customer_id,
    customer.name,
    order.order_id,
    order.amount,
    order.status

FROM (
    SELECT
        from_json(
            payload,
            'STRUCT<
                customer_id: BIGINT,
                name: STRING,
                orders: ARRAY<
                    STRUCT<
                        order_id: STRING,
                        amount: DOUBLE,
                        status: STRING
                    >
                >
            >'
        ) AS customer

    FROM main.demo.customer_array_json
) parsed

LATERAL VIEW EXPLODE(customer.orders) exploded AS order;

Query the view:


SELECT *
FROM main.demo.v_customer_orders;

ARRAY is useful when a JSON document contains lists of repeating elements, such as:

  • Customer orders

  • Product items

  • Transaction lines

  • Event collections

  • API result lists

One important consideration: EXPLODE changes the granularity of the data. If one customer has five orders, the resulting dataset contains five rows for that customer.

Geen opmerkingen:

Een reactie posten