Home > other >  How to parse nested JSON with no array in BigQuery?
How to parse nested JSON with no array in BigQuery?

Time:01-03

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?

CodePudding user response:

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

enter image description here

  • Related