[英]SQL - Extract JSON from Array
I'm writing some queries with Google BigQuery. 我正在用Google BigQuery写一些查询。 I want to extract the JSON from an array.
我想从数组中提取JSON。 Somehow I can extract it.
不知怎的,我可以提取它。 And @Mikhail Berlyant helped me here .
@Mikhail Berlyant 在这里帮助了我。 But now the problem is in extracting JSON from the array without Duplicates.
但现在的问题是从没有Duplicates的数组中提取JSON。
WITH
cte AS (
SELECT
labels,
cost
FROM
BILLING.gcp_billing_export_v1)
SELECT
la,
cost
FROM
cte,
UNNEST(labels) AS la
See the cost box, the COST value is repeated twice, because we have 2 KEY, VALUE pairs in the array. 参见成本框,COST值重复两次,因为我们在数组中有2个KEY,VALUE对。
So while doing sum(cost)
with the group by la.key
then I'm getting the wrong value. 因此,当通过
la.key
与组进行sum(cost)
,我得到了错误的值。
Can anyone help me with this? 谁能帮我这个?
Below is for BigQuery Standard SQL 以下是BigQuery Standard SQL
#standardSQL
SELECT
description,
ARRAY(
SELECT AS STRUCT
JSON_EXTRACT_SCALAR(kv, '$.key') key,
JSON_EXTRACT_SCALAR(kv, '$.value') value
FROM UNNEST(SPLIT(labels, '},{')) kv_temp,
UNNEST([CONCAT('{', REGEXP_REPLACE(kv_temp, r'^\[{|}]$', ''), '}')]) kv
) labels,
cost
FROM `project.dataset.table`
You can test, play with above using excerpt of dummy data from your question as below 您可以使用以下问题中的虚拟数据摘录测试,播放上面的内容
#standardSQL
WITH `project.dataset.table` AS (
SELECT 'aaa' description, '[{"key":"application","value":"scaled-server"},{"key":"department","value":"hrd"}]' labels, 0.323316 cost UNION ALL
SELECT 'bbb' description, '[{"key":"application2","value":"scaled-server2"},{"key":"department2","value":"hrd2"}]' labels, 0.342825 cost
)
SELECT
description,
ARRAY(
SELECT AS STRUCT
JSON_EXTRACT_SCALAR(kv, '$.key') key,
JSON_EXTRACT_SCALAR(kv, '$.value') value
FROM UNNEST(SPLIT(labels, '},{')) kv_temp,
UNNEST([CONCAT('{', REGEXP_REPLACE(kv_temp, r'^\[{|}]$', ''), '}')]) kv
) labels,
cost
FROM `project.dataset.table`
with result 结果
Row description labels.key labels.value cost
1 aaa application scaled-server 0.323316
department hrd
2 bbb application2 scaled-server2 0.342825
department2 hrd2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.