简体   繁体   中英

Map in BigQuery (dynamic keys)

In bigquery, if we are interested in constructing json output, we can usually use struct for json object when the keys are known beforehand.

SELECT TO_JSON_STRING(STRUCT(key1))
FROM (SELECT "val1" as key1 UNION ALL
      SELECT "val2" as key1)


Result
{"key1":"val1"}
{"key1":"val2"}

But in the case where the keys are dynamic, we really want a map type, similar to the avro map type

For example

SELECT *
FROM (SELECT "key1" as key, "val1" as val UNION ALL
      SELECT "key2" as key, "val2" as val)

should return
{"key1": "val1", "key2": "val2"}

is there anyway to achieve this using BigQuery SQL?

Below is for BigQuery Standard SQL

Something simple like below should produce expected result

#standardSQL
WITH `project.dataset.table` AS (
  SELECT "key1" AS key, "val1" AS val UNION ALL
  SELECT "key2" AS key, "val2" AS val
)
SELECT '{' || STRING_AGG(REPLACE(TRIM(FORMAT('%T', t), '()'), '", "', '": "'), ', ') || '}' AS return
FROM `project.dataset.table` t

with output

Row return   
1   {"key1": "val1", "key2": "val2"}     

You can use Dynamic SQL to generate JSON string:

DECLARE
  JSONSTR STRING;
SET
  JSONSTR = (
  SELECT
    '{' || STRING_AGG('"' || key || '": "' || val || '"', ', ') || '}'
  FROM (
    SELECT *
    FROM (SELECT "key1" AS key, "val1" AS val
          UNION ALL
          SELECT "key2" AS key, "val2" AS val)));
EXECUTE IMMEDIATE
  FORMAT("""SELECT '%t'""",JSONSTR);

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