在 Google BigQuery 中查询 JSON 字符串
Posted
技术标签:
【中文标题】在 Google BigQuery 中查询 JSON 字符串【英文标题】:Query JSON string in Google BigQuery 【发布时间】:2021-03-13 21:50:57 【问题描述】:我在 BigQuery 的一列中有以下 JSON 值,我正在努力将数据提取到列中。
我们的价值:
["availableSlots":0,"date":"03/16/2021","day":"Tue","slotType":"AM","totalSlots":5,"availableSlots":2,"date":"03/16/2021","day":"Tue","slotType":"PM","totalSlots":5,"availableSlots":5,"date":"03/17/2021","day":"Wed","slotType":"AM","totalSlots":5,"availableSlots":5,"date":"03/17/2021","day":"Wed","slotType":"PM","totalSlots":5,"availableSlots":5,"date":"03/18/2021","day":"Thu","slotType":"AM","totalSlots":5]
我正在尝试获取 availableSlots 的第一个和第二个值。
我确实尝试过 JSON_EXTRACT_SCALAR 和 JSON_EXTRACT
感谢您的帮助。
【问题讨论】:
【参考方案1】:见下例
with `project.dataset.table` as (
select '["availableSlots":0,"date":"03/16/2021","day":"Tue","slotType":"AM","totalSlots":5,"availableSlots":2,"date":"03/16/2021","day":"Tue","slotType":"PM","totalSlots":5,"availableSlots":5,"date":"03/17/2021","day":"Wed","slotType":"AM","totalSlots":5,"availableSlots":5,"date":"03/17/2021","day":"Wed","slotType":"PM","totalSlots":5,"availableSlots":5,"date":"03/18/2021","day":"Thu","slotType":"AM","totalSlots":5]' col
)
select
json_extract_scalar(json, '$.availableSlots') as availableSlots,
json_extract_scalar(json, '$.date') as date,
json_extract_scalar(json, '$.day') as day,
json_extract_scalar(json, '$.slotType') as slotType,
json_extract_scalar(json, '$.totalSlots') as totalSlots
from `project.dataset.table`,
unnest(json_extract_array(col, '$')) json
有输出
【讨论】:
以上是关于在 Google BigQuery 中查询 JSON 字符串的主要内容,如果未能解决你的问题,请参考以下文章
Google-cloud-dataflow:无法通过带有“BigQueryDisposition.WRITE_TRUNCATE”的“WriteToBigQuery/BigQuerySink”将 jso
在 Google BigQuery 中查询 JSON 字符串
如何在 Google BigQuery 中设置用户的查询配额