简体   繁体   English

将 JSON 数组取消嵌套为多行 - BigQuery

[英]Unnest a JSON array in as multiple rows - BigQuery

I have a table that looks like this我有一张看起来像这样的桌子

| id | title    |  metadata                                      |
| 1  | apples   | [{"tags": [200,211], "categories": [313,412]}] |
| 2  | oranges  | [{"tags": [311,112], "categories": [616,712]}] |

I want to unnest the metadata so the results is one row per tag我想取消嵌套元数据,因此结果是每个标签一行

| id | title     |  tag |
| 1  | apples    |  200  | 
| 1  | apples    |  212  |
| 2  | oranges   |  313  |  
| 2  | oranges   |  112  |

I know I need to use JSON_EXTRACT_ARRAY to and UNNEST, but since there is another array nested inside JSON I'm a bit confused as to how these should interact?我知道我需要使用JSON_EXTRACT_ARRAY和 UNNEST,但是由于有另一个嵌套在 JSON 中的数组,我对这些应该如何交互感到有些困惑?

Consider below approach考虑以下方法

select * except(metadata) from data,
unnest(split(trim(json_extract(trim(metadata, '[]'), '$.tags'), '[]'))) tag          

if applied to sample data in your question - output is如果应用于您问题中的样本数据 - 输出是

在此处输入图片说明

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

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