简体   繁体   中英

How can I only get keys from an array in BigQuery

I have an event table in BigQuery. I have two columns: event_name and event_parameters. I need to export parameter keys.

I'm looking for something similar to

dict.keys()

from pyton.

for example:

event_name event_parameters
event_a {"from": "main_page","ID":"123"}
event_b {"ID":"242","custom_value":"true"

output table:

event_name event_parameters
event_a {"from","ID"}
event_b {"ID","custom_value"

You can extract the keys from a json column using the following SQL code as a template:

WITH data AS(
SELECT '{"from":"main_page","ID":"123"}' AS params
)

SELECT keys from(
  select
    params,
    array(select trim(split(kv, ':')[offset(0)]) from t.kv kv) as keys,
    array(
      select as struct 
        trim(split(kv, ':')[offset(0)]) as key, 
        trim(split(kv, ':')[offset(1)]) as value
      from t.kv kv
    ) as key_value
  from data, 
  unnest([struct(split(translate(params, '{}"', '')) as kv)]) t
)

Credit to @Mikhail Berlyant for providing the original code for the following solution: BigQuery: extract keys from json object, convert json from object to key-value array

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM