We have loaded json blobs in a String field in a Bigquery table. I need to create a view (using standard sql)over the table that would extract the array field as a bigquery
A little bit more brute-force version - I think easier to read and modify/adjust if needed
#standardSQL
WITH `yourTable` AS (
SELECT '{"order_id":"123456","customer_id":"2abcd", "items":[{"line":"1","ref_ids":["66b56e60","9e7ca2b7"],"sku":"1111","amount":40 },{"line":"2","ref_ids":["7777h0","8888j0"],"sku":"2222","amount":10 }]}' AS json_blob
)
SELECT
JSON_EXTRACT_SCALAR(json_blob, '$.order_id') AS order_id,
JSON_EXTRACT_SCALAR(json_blob, '$.customer_id') AS customer_id,
ARRAY(
SELECT STRUCT(
JSON_EXTRACT_SCALAR(split_items, '$.line') AS line,
SPLIT(REGEXP_REPLACE(JSON_EXTRACT (split_items, '$.ref_ids'), r'[\[\]\"]', '')) AS ref_ids,
JSON_EXTRACT_SCALAR(split_items, '$.sku') AS sku,
JSON_EXTRACT_SCALAR(split_items, '$.amount') AS amount
)
FROM (
SELECT CONCAT('{', REGEXP_REPLACE(split_items, r'^\[{|}\]$', ''), '}') AS split_items
FROM UNNEST(SPLIT(JSON_EXTRACT(json_blob, '$.items'), '},{')) AS split_items
)
) AS items
FROM `yourTable`