从 Bigquery 中的 json 获取数组
Posted
技术标签:
【中文标题】从 Bigquery 中的 json 获取数组【英文标题】:Get array from json in Bigquery 【发布时间】:2021-04-20 18:31:07 【问题描述】:我正在尝试从 BigQuery 中的 JSON 获取数据。此 JSON 存储在单列表中。 到目前为止,我只能获得“变量”数组,其中包含以下内容:
Select JSON_QUERY_ARRAY(Column1, '$.sessions[0].variables') FROM Table
如何获取其他值/数组(sessionMessage 和事件)?我不能让它工作。。 我试过了:
JSON_VALUE(Column1, '$.sessions[0].conversation')
JSON_QUERY_ARRAY(Column1, '$.sessions[0].sessionMessages')
但我只得到空值(原始 json 在这个数组中有值..)
"fromDate":"2020-04-10T23:47:17.161Z",
"pageRows":151,
"sessions":[
"variables":[],
"sessionDate":"2020-04-10T23:47:17.161Z",
"botMessages":2,
"userHasTalked":"true",
"topics":[
"TOPIC1"
],
"sessionId":"WXXXSXSXSXXXQ_2020-01-00T23:47:17.161Z",
"platformContactId":"XXXXXXX-XXXXXXX-XXXXXXXXXXXXXX",
"sessionMessages":[.....],
"queues":[
"QUEUE1",
"QUEUE2"
],
"customerId":"SSDSDS",
"userMessages":2,
"operatorMessages":1,
"sessionMessagesQty":2,
"sessionStartingCause":"Organic",
"channelId":"IDCHANEL",
"conversation":"https://url.com",
"events":[.....]
],
"toDate":"2020-04-10T23:47:17.161Z",
"hasMore":true,
"pageToken":"XXXXXXXXXXXXXX"
【问题讨论】:
【参考方案1】:您使用的函数和 JSONPath 没有任何问题,但是您的示例 JSON 文件有一些意想不到的东西,例如 [.....],
删除/替换这些并且下面的查询可以正常工作:
WITH a as (select
"""
"fromDate":"2020-04-10T23:47:17.161Z",
"pageRows":151,
"sessions":[
"variables":[],
"sessionDate":"2020-04-10T23:47:17.161Z",
"botMessages":2,
"userHasTalked":"true",
"topics":[
"TOPIC1"
],
"sessionId":"WXXXSXSXSXXXQ_2020-01-00T23:47:17.161Z",
"platformContactId":"XXXXXXX-XXXXXXX-XXXXXXXXXXXXXX",
"sessionMessages":[1,2,3],
"queues":[
"QUEUE1",
"QUEUE2"
],
"customerId":"SSDSDS",
"userMessages":2,
"operatorMessages":1,
"sessionMessagesQty":2,
"sessionStartingCause":"Organic",
"channelId":"IDCHANEL",
"conversation":"https://url.com",
"events":[],
],
"toDate":"2020-04-10T23:47:17.161Z",
"hasMore":true,
"pageToken":"XXXXXXXXXXXXXX"
""" data)
SELECT JSON_VALUE(data, '$.sessions[0].conversation'),
JSON_QUERY_ARRAY(data, '$.sessions[0].sessionMessages')
FROM a;
【讨论】:
以上是关于从 Bigquery 中的 json 获取数组的主要内容,如果未能解决你的问题,请参考以下文章