Skip to content
Advertisement

fetch key value pairs from array objects in sql BigQuery

I need to parse the column mapping and fetch the key value pairs from the second object in array. I would like to create new columns with the fetched data. The data looks like this:

row                 mapping 
1       [{'adUnitCode': ca, 'id': 35, 'name': ca}, {'adUnitCode': hd, 'id': 11, 'name': HD}]
2       [{'adUnitCode': bb, 'id': 56, 'name': jk}, {'adUnitCode': hm, 'id': 12, 'name': HM}]
3       [{'adUnitCode': gh, 'id': 78, 'name': ff}, {'adUnitCode': hk, 'id': 13, 'name': HK}]

The desired output:

row                 adUnitCode                  id                  name
1                     hd                       11                     HD
2                     hm                       12                     HM
3                     hk                       13                     HK

Advertisement

Answer

Below is one of the approaches (BigQuery Standard SQL)

#standardSQL
select 
  json_extract_scalar(second_object, "$.adUnitCode") as adUnitCode,
  json_extract_scalar(second_object, "$.id") as id,
  json_extract_scalar(second_object, "$.name") as name
from `project.dataset.table`, unnest(
  [json_extract_array(regexp_replace(mapping, r"(: )(w+)(,|})", "\1'\2'\3"))[safe_offset(1)]]
) as second_object

if to apply above to sample data from your question – result is

enter image description here

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement