Sub topic 5
Turning JSON Arrays and Objects into Rows
Assume you know SELECT, joins, aliases, and basic JSON syntax. The examples use the orders table from the earlier setup, where details is a jsonb column containing an items array and a shipping object.
JSON is useful for storing a document, but SQL works best with rows. PostgreSQL provides set-returning functions that expand one JSON array or object into several rows. You can then filter, join, group, and aggregate the expanded data like any other query result.
Expanding an array
jsonb_array_elements takes a JSON array and returns one jsonb value for each element. In this example, each order can produce several result rows:
SELECT o.id,
o.customer,
item ->> 'sku' AS sku,
(item ->> 'qty')::int AS quantity
FROM orders AS o
CROSS JOIN LATERAL jsonb_array_elements(o.details -> 'items') AS item;CROSS JOIN LATERAL means that the function can use columns from the row on its left. PostgreSQL evaluates the function separately for each order. If an order has three items, it contributes three rows. If its array is empty, it contributes no rows.
The item alias represents one JSON object. The ->> operator extracts a field as text, and the cast converts the quantity to an integer. Cast values before arithmetic, comparisons, or numeric aggregation.
You can preserve the array position with WITH ORDINALITY. Positions start at one:
SELECT o.id,
item_position,
item ->> 'sku' AS sku
FROM orders AS o
CROSS JOIN LATERAL jsonb_array_elements(o.details -> 'items')
WITH ORDINALITY AS expanded(item, item_position);jsonb_array_elements_text is convenient when each array element is a scalar, such as a list of tags:
SELECT o.id, tag
FROM orders AS o
CROSS JOIN LATERAL jsonb_array_elements_text(o.details -> 'tags') AS expanded(tag);Use jsonb_array_elements when elements are objects or when you need to keep JSON values. Use the _text version when the elements are scalar values and text is sufficient.
Expanding an object
jsonb_each returns one row for every top-level key in a JSON object. It returns two columns: key as text and value as jsonb.
SELECT o.id,
attribute.key,
attribute.value
FROM orders AS o
CROSS JOIN LATERAL jsonb_each(o.details -> 'shipping') AS attribute;For a shipping object such as {"city": "Oslo", "country": "Norway"}, the query returns one row for city and one row for country.
Use jsonb_each_text when you want every value as text:
SELECT o.id,
attribute.key,
attribute.value
FROM orders AS o
CROSS JOIN LATERAL jsonb_each_text(o.details -> 'shipping') AS attribute;| Function | Input | Result per input document |
|---|---|---|
| `jsonb_array_elements` | JSON array | One `jsonb` value per array element |
| `jsonb_array_elements_text` | JSON array | One text value per scalar element |
| `jsonb_each` | JSON object | A text key and `jsonb` value per member |
| `jsonb_each_text` | JSON object | A text key and text value per member |
Expanding nested structures
You can chain lateral expansions. The first expansion produces an item, and the second expansion can inspect an object inside that item:
SELECT o.id,
item ->> 'sku' AS sku,
option.key,
option.value
FROM orders AS o
CROSS JOIN LATERAL jsonb_array_elements(o.details -> 'items') AS item
CROSS JOIN LATERAL jsonb_each(item -> 'options') AS option;This query returns only items that contain an options object with at least one member. Keep the aliases clear because each lateral function changes the row shape.
Exercise
Which function and join form should you use to return one row per object in the items array for every order?
Show answer
Use jsonb_array_elements(o.details -> 'items') with CROSS JOIN LATERAL, for example: FROM orders AS o CROSS JOIN LATERAL jsonb_array_elements(o.details -> 'items') AS item.