简体   繁体   中英

How to parse nested JSON with no array in BigQuery?

I have a table with a column filled with stringified JSON so I have a JSON with an example as such:

{
    "id_1": {
        "age": "10",
        "size": "M",
    },
    "id_2": {
        "age": "20",
        "size": "L",
    },
    ...
}

I want to parse it in BigQuery so that I can have a table result as such:

id age size
id_1 10 M
id_2 20 L

I want the id key to be present in the table. How can I achieve this?

Below using custom UDF might be an option to address your problem.

CREATE TEMP FUNCTION flatten_json(json STRING)
RETURNS ARRAY<STRUCT<id STRING, age STRING, size STRING>>
LANGUAGE js AS """
  result = [];
  for (const [key, value] of Object.entries(JSON.parse(json))) {
    value["id"] = key; result.push(value);
  }
  return result;
""";

WITH sample_table AS (
  SELECT '''{
    "id_1": {
        "age": "10",
        "size": "M"
    },
    "id_2": {
        "age": "20",
        "size": "L"
    }
  }''' json
)
SELECT flattened.*
  FROM sample_table, UNNEST(flatten_json(json)) flattened;

Query results

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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