简体   繁体   English

具有 json 个值的大查询 unnest 数组

[英]Big query unnest array with json values

Lets consider the following table on Google BigQuery:让我们考虑一下 Google BigQuery 上的下表:

WITH example AS (
    SELECT 1 AS id, ["{\"id\":1, \"name\":\"AAA\"}", "{\"id\":2, \"name\":\"BBB\"}","{\"id\":3, \"name\":\"CCC\"}"] 
    UNION ALL
    SELECT 2 AS id, ["{\"id\":5, \"name\":\"XXX\"}", "{\"id\":6, \"name\":\"ZZZ\"}"] 
)
SELECT * 
FROM example;

在此处输入图像描述

I would like to compose a query that will return names with their parent row's id.我想编写一个查询,该查询将返回名称及其父行的 ID。

like:喜欢:

在此处输入图像描述

I tried using unnest with json functions and I just cant make this right.我尝试将 unnest 与 json 函数一起使用,但我无法做到这一点。

Can anyone help me?谁能帮我?

Thanks Ido谢谢伊多

According to your query, you already have json elements in your array.根据您的查询,您的数组中已经有 json 个元素。 So with the use of unnest, you can use a json function like json_value to extract the name attribute of your elements.因此,使用 unnest,您可以使用json function 之类的json_value来提取元素的名称属性。

select 
    id,
    json_value(elt, '$.name')
from example, unnest(r) as elt;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM