MySQL JSON_TABLE: Shredding JSON Columns into Relational Rows

MySQL JSON_TABLE: turn JSON into rows you can join and aggregate

Data in a JSON column is easy to store and awkward to report on: a JSON array of order lines from an external system cannot be summed with SUM() or joined to wp_posts as it is. JSON_TABLE() turns a JSON document into a temporary table inside a query, with one row per array element and typed columns, so normal SQL works on it.

Problem: A plugin stores the raw payload of every marketplace order as JSON in wp_ha_marketplace_orders.payload. The shop wants a report of quantities sold per SKU, but the lines are an array inside the JSON, and the current PHP script decodes 50,000 payloads in a loop.

Solution: Use JSON_TABLE( payload, '$.lines[*]' COLUMNS ( ... ) ) in a lateral join to expand each order’s lines into rows with sku, qty and price columns, then group, sum and join them to the products in plain SQL. Requires MySQL 8.0.4+ or MariaDB 10.6+.

The data

-- payload of one row:
-- {"id":"MP-1001","date":"2026-04-12","lines":[{"sku":"MUG-BLUE","qty":2,"price":12.5},{"sku":"TEA-01","qty":1,"price":8}]}

Expand the array into rows

SELECT o.id AS order_id, l.sku, l.qty, l.price
FROM wp_ha_marketplace_orders AS o,
     JSON_TABLE(
         o.payload, '$.lines[*]'
         COLUMNS (
             line_no FOR ORDINALITY,
             sku     VARCHAR(64)   PATH '$.sku',
             qty     INT           PATH '$.qty'   DEFAULT '0' ON EMPTY,
             price   DECIMAL(10,2) PATH '$.price' NULL ON ERROR
         )
     ) AS l;

The path $.lines[*] selects the array; every element becomes one row. Each column has a SQL type and a path relative to the element. FOR ORDINALITY numbers the rows. DEFAULT ... ON EMPTY fills missing keys, NULL ON ERROR handles values that cannot be converted to the type, so one malformed line does not break the report. The comma join with the table function is a lateral join: JSON_TABLE sees the columns of the current row o.

Report per SKU, joined to the products

SELECT l.sku,
       SUM(l.qty)            AS sold,
       SUM(l.qty * l.price)  AS revenue,
       p.ID                  AS product_id,
       p.post_title          AS product
FROM wp_ha_marketplace_orders AS o,
     JSON_TABLE( o.payload, '$.lines[*]' COLUMNS (
         sku VARCHAR(64) PATH '$.sku',
         qty INT PATH '$.qty',
         price DECIMAL(10,2) PATH '$.price'
     ) ) AS l
LEFT JOIN wp_wc_product_meta_lookup AS pl ON pl.sku = l.sku
LEFT JOIN wp_posts AS p ON p.ID = pl.product_id
WHERE JSON_UNQUOTE( JSON_EXTRACT( o.payload, '$.date' ) ) >= '2026-04-01'
GROUP BY l.sku, p.ID, p.post_title
ORDER BY sold DESC;

The SKU is matched against WooCommerce’s lookup table wc_product_meta_lookup, which has an indexed sku column, instead of searching wp_postmeta. A LEFT JOIN keeps SKUs that do not exist in the shop, which a reconciliation report should show.

Nested arrays

COLUMNS (
    sku VARCHAR(64) PATH '$.sku',
    NESTED PATH '$.options[*]' COLUMNS (
        option_name  VARCHAR(64) PATH '$.name',
        option_value VARCHAR(64) PATH '$.value'
    )
)

NESTED PATH expands an array inside each element; a line with three options produces three rows.

Performance

JSON_TABLE parses the JSON of every row it reads, so filter the outer table first (by date, by status) using normal indexed columns, and expand only what is needed. For a report that runs daily on a growing table, a STORED generated column for the order date and an index on it keep the outer filter fast: generated columns for values inside JSON. Storing variable data as JSON in the first place: a JSON column for flexible data.

NOTE: MariaDB added JSON_TABLE in 10.6 with the same syntax; earlier MariaDB versions and MySQL 5.7 do not have it, and there the expansion must happen in PHP. The wc_product_meta_lookup table exists since WooCommerce 3.6.