【问题标题】:Split json data into separate row result in Bigquery在 Bigquery 中将 json 数据拆分为单独的行结果
【发布时间】:2020-06-10 09:58:00
【问题描述】:

我正在编写一个 bigquery 代码来将 JSON 数据集拆分为更结构化的表。

JSON_data_set 看起来像这样

Row | createdon | result
1   | 24022020  | {"searchResult": {"searchAccounts": [{"chainName": "xyxvjw", "address": {"name": "xyxvjw - ythji", "combined_city": "uptown", "combined_address": "1 downtown, uptown, 09728", "city": "uptown"}, "products": ["pin", "needle", "cloth"]}},{"chainName": "pwiewhds", "address": {"name": "pwiewhds - oujsus", "combined_city": "over the river", "combined_address": "100 under bridge, over the river, 19920", "city": "over the river"}, "products": ["tape", "stapler"]}}],"searchID": "3abci832832o0"}}
2   | 25020202  | {"searchResult": {"searchAccounts": [{"chainName": "xyxvjw2029", "address": {"name": "xyxvjw2029 - ythji", "combined_city": "uptown", "combined_address": "1 downtown, uptown, 09728", "city": "uptown"}, "products": ["pin", "needle", "cloth"]}},{"chainName": "pwiewhds8972", "address": {"name": "pwiewhds8972 - oujsus", "combined_city": "over the river", "combined_address": "100 under bridge, over the river, 19920", "city": "over the river"}, "products": ["tape", "stapler"]}}],"searchID": "3abci832832o0"}}

结果列的每一行都有很多后续的账户详情。 能够使用以下代码取消嵌套数据以获取列数据,例如链名称和地址。但是,当我尝试调用分解的字段列时,它给了我错误 Cannot access field _field_1 on a value with type ARRAY-STRUCT-STRING, STRING>>

如何在不绑定到 json 行列的情况下,将由 json 数据创建的列分成单独的列和行?

CREATE TEMP FUNCTION json2array(json STRING)
RETURNS ARRAY<STRING>
LANGUAGE js AS """
   if (json !== null) {
    return JSON.parse(json).map(x=>JSON.stringify(x));
   }
"""; 

SELECT * EXCEPT(chains),
  ARRAY(SELECT AS STRUCT JSON_EXTRACT_SCALAR(x, '$.chainName'), JSON_EXTRACT_SCALAR(x, '$.address.combined_address') FROM UNNEST(chains) x WHERE JSON_EXTRACT_SCALAR(x, '$.chainName') IS NOT NULL) chain_names
FROM (
  SELECT *,
     json2array(
      JSON_EXTRACT(result, '$.searchResult.searchAccounts')
     ) chains
  FROM json_data_set
)

【问题讨论】:

    标签: arrays json google-bigquery


    【解决方案1】:

    只需要以不同的方式编写查询来实现单个列

    CREATE TEMP FUNCTION json2array(json STRING)
    RETURNS ARRAY<STRING>
    LANGUAGE js AS """
       if (json !== null) {
        return JSON.parse(json).map(x=>JSON.stringify(x));
       }
    """; 
    
    WITH chain_name AS (
      SELECT 
         *,
         json2array(
          JSON_EXTRACT(result, '$.searchResult.searchMerchants')
         ) chains
      FROM json_data_set
    )
    SELECT AS STRUCT 
       JSON_EXTRACT_SCALAR(x, '$.chainName') chainName, 
       JSON_EXTRACT_SCALAR(x, '$.address.combined_address') combined_address
    FROM chain_name, UNNEST(chains) x
    WHERE JSON_EXTRACT_SCALAR(x, '$.chainName') IS NOT NULL
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-02
      • 1970-01-01
      • 1970-01-01
      • 2018-12-10
      • 2020-12-04
      • 1970-01-01
      • 2022-01-14
      • 1970-01-01
      相关资源
      最近更新 更多