How to extract all the keys in a JSON object with BigQuery

前端 未结 4 1897
甜味超标
甜味超标 2020-12-30 10:24

BigQuery has facilities to parse JSON in real-time interactive queries: Just store the JSON encoded object as a string, and query in real time, with functions like JSON_EXTR

4条回答
  •  孤独总比滥情好
    2020-12-30 11:01

    Here's something that uses Standard SQL:

    CREATE TEMP FUNCTION jsonObjectKeys(input STRING)
    RETURNS Array
    LANGUAGE js AS """
      return Object.keys(JSON.parse(input));
    """;
    WITH keys AS (
      SELECT
        jsonObjectKeys(myColumn) AS keys
      FROM
        myProject.myTable
      WHERE myColumn IS NOT NULL
    )
    SELECT
      DISTINCT k
    FROM keys
    CROSS JOIN UNNEST(keys.keys) AS k
    ORDER BY k
    

提交回复
热议问题