sql - Bigquery - json_array 从嵌套数组中提取和计算元素

标签 sql arrays json google-bigquery

我的表有一个 JSON 字段,如下所示。可以有任意数量的菜单类别和任意数量的项目

{
"menu": {
    "salad": [
        {
            "item": "russian salad"
        },
        {
            "item": "french salad"
        }
    ],
    "soups": [
        {
            "item": "french soup"
        },
        {
            "item": "english soup"
        },
        {
            "item": "english soup"
        }
    ]
}

可以有任意数量的菜单类别(沙拉、汤)以及每个类别中任意数量的项目

salad | 2
soups | 3

我查看了我的 own previous question ,但不确定如何修改以适应任意数量的类别和项目?

最佳答案

考虑以下方法

select key, array_length(split(values)) items
from `project.dataset.table`,
unnest(`bqutil.fn.json_extract_keys`(json_extract(col, '$.menu'))) key with offset 
join unnest(`bqutil.fn.json_extract_values`(json_extract(col, '$.menu'))) values with offset 
using(offset)          

如果应用于问题中的示例数据 - 输出为

enter image description here

关于sql - Bigquery - json_array 从嵌套数组中提取和计算元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70596872/

相关文章:

php - 嵌套查询语法改进

mysql - Symfony 中带有 ORDER BY 匹配条件的自定义 SQL 查询

sql - 从 C 中的 oracle DB 中的字段中提取有效 IP

arrays - 按特定的自定义字段对 Doctrine ArrayCollection 进行排序

与将输入输入到空数组中感到困惑。

json - 当我尝试用 go lang 调用值时出错

sql - 删除表中的随机行,但保留固定数量的条目

javascript - Angular js : how to orderBy an array without keys?

ios - JSON 数据未显示 - 出现错误

c# - JSON 主体未被 NancyModule 反序列化