从BigQuery中的JSON数组中提取值
我试图从JSON数组中提取值,如下所示从BigQuery中的JSON数组中提取值,json,google-cloud-platform,google-bigquery,Json,Google Cloud Platform,Google Bigquery,我试图从JSON数组中提取值,如下所示 with `project.dataset.table` as ( select '{"fruit":[{"apples":"5","oranges":"10","pear":"20"}, {"apples":"5","o
with `project.dataset.table` as (
select '{"fruit":[{"apples":"5","oranges":"10","pear":"20"},
{"apples":"5","oranges":"4"},
{"apples":"5","oranges":"9","pear":"40"}]}' as json union all
select '{"fruit":[{"lettuce":"7","kale": "8"}]}'
)
select json, if(regexp_contains(json, '"apples":"5"'), (SELECT
ARRAY_AGG(json_extract_scalar(arr, '$.oranges') ignore nulls)
from
UNNEST(json_extract_ARRAY(json, '$.fruit')) as arr ), null) as oranges,
if(regexp_contains(json, '"apples":"5"'), (SELECT
ARRAY_AGG(json_extract_scalar(arr, '$.pear') ignore nulls)
from
UNNEST(json_extract_ARRAY(json, '$.fruit')) as arr ), null) as pear,
from `project.dataset.table`
它给出如下输出
我期待像这样的输出
json
橘子
梨
{“水果”:[{“苹果”:“5”,“桔子”:“10”,“梨”:“20”},{“苹果”:“5”,“桔子”:“4”},{“苹果”:“5”,“桔子”:“9”,“梨”:“40”}
10
20
4.
无效的
9
40
{“水果”:[{“莴苣”:“7”,“羽衣甘蓝”:“8”}]}。
无效的
无效的
当与
如果最终查询结果中的数组包含
空元素
您可以尝试使用如下字符串'null'
:
with `project.dataset.table` as (
select '{"fruit":[{"apples":"5","oranges":"10","pear":"20"},{"apples":"5","oranges":"4"},{"apples":"5","oranges":"9","pear":"40"}]}' as json union all
select '{"fruit":[{"lettuce":"7","kale": "8"}]}'
)
select
json,
if( regexp_contains(json, '"apples":"5"'),
(SELECT ARRAY_AGG(ifnull(json_extract_scalar(arr, '$.oranges'), 'null')) from UNNEST(json_extract_ARRAY(json, '$.fruit')) as arr ),
['null']
) as oranges,
if( regexp_contains(json, '"apples":"5"'),
(SELECT ARRAY_AGG(ifnull(json_extract_scalar(arr, '$.pear'), 'null')) from UNNEST(json_extract_ARRAY(json, '$.fruit')) as arr ),
['null']
) as pear,
from `project.dataset.table`
考虑以下方法,更明确地对齐各个条目
select json,
array(
select
if(
json_extract_scalar(val, '$.apples') = '5',
struct(
json_extract_scalar(val, '$.oranges') as oranges,
json_extract_scalar(val, '$.pear') as pear
),
struct(null, null)
)
from t.arr val
) fruits
from `project.dataset.table`,
unnest([struct(json_extract_array(json, '$.fruit') as arr)]) t
与两个数组相比,它的详细程度更低,并将结果作为重复记录输出如果应用于问题中的样本数据,则输出为 如果您真的需要将输出保持为单独的列,请使用下面的命令
select json,
array(
select
if(
json_extract_scalar(val, '$.apples') = '5',
ifnull(json_extract_scalar(val, '$.oranges'), '0'),
'0'
)
from t.arr val
) as oranges,
array(
select
if(
json_extract_scalar(val, '$.apples') = '5',
ifnull(json_extract_scalar(val, '$.pear'), '0'),
'0'
)
from t.arr val
) as pear
from `project.dataset.table`,
unnest([struct(json_extract_array(json, '$.fruit') as arr)]) t
有输出
谢谢你的回答:)谢谢你的帮助:)这对我很有效!