I have a column in my BigQuery with various different messages in a simple one-depth JSON format which I would like to extract into a STRUCT. The input table looks like
and should be transformed into
I am aware of BigQuery json-functions such as JSON_EXTRACT
as posted for example here. However, this approach is out of question since in production there like 100 different senders. Therefore, I need to be able to extract those JSONs dynamically without specifying their keys manually.
I have been playing around with regex as shown here
WITH input_table AS (
SELECT
1 AS Row,
20210101 AS Date,
'Sender1' AS Sender,
'{"param1": 123, "param2": 456, "param3": 78, "value1": 42, "label1": "hello", "timestamp": 1234567890}' AS Message
UNION ALL SELECT
2 AS Row,
20210101 AS Date,
'Sender2' AS Sender,
'{"value1": 4, "label1": "myLabel", "label2": "yourLabel"}' AS Message
UNION ALL SELECT
3 AS Row,
20210102 AS Date,
'Sender1' AS Sender,
'{"param1": 12, "param2": 90, "param3": 55, "value1": 11, "label1": "there", "timestamp": 1235555555}' AS Message
)
SELECT
CONCAT("SELECT ", key, " AS key, JSON_EXTRACT_SCALAR(Message, '$.", key, "') AS ", key, " FROM input_table")
FROM input_table, unnest(regexp_extract_all(regexp_replace(JSON_EXTRACT(Message, '$'), r':{.*?}+', ''), r'"(.*?)":')) key
but apart from the fact that my regex is still a bit off, I am struggling to convert these statements into a STRUCT.