Bigquery 获取 json 键名

Bigquery Get json key name

我有一个 BigQuery table,其中有一列包含 JSON 字符串。在 JSON 中,可能有一个名为“person”或“corp”或“sme”的键。我想要 运行 一个查询,该查询将 return JSON 中存在哪些可能的键并将其存储在新列中。

下面是'class'列的数据,在BQ中各为一长串。第一级键名可以等于“corp”、“sme”或“person”(参见下面的示例)。

示例 1

{
  "corp": {
    "address": {
      "city": "London",
      "countryCode": "gb",
      "streetAddress": [
        "Fairlop road"
      ],
      "zip": "e111bn"
    },
    "cin": 1234567420,
    "title": "Demo Corp"
  }
}

示例 2

{
  "person": {
    "address": {
      "city": "Madrid",
      "countryCode": "es",
      "streetAddress": [
        "Some street 1"
      ],
      "zip": "z1123ab"
    },
    "cin": 1234567411,
    "title": "Demo Person"
  }
}

我试过使用 json_xxx 函数,但它们需要指定 json_path。我有兴趣获取 json_path 名称以创建一个新列 (cust_type),其中列出每一行的公司、中小企业、人员。

example cust_type
1 corp
2 person

这是我的第一个问题,请多多包涵!谢谢

也许我们可以使用 JSON_EXTRACT 函数查看该字段是否存在(不为空)。示例测试可能是:

SELECT CASE
    WHEN JSON_EXTRACT(json_text, '$.corp') is not null then 'corp'
    WHEN JSON_EXTRACT(json_text, '$.person') is not null then 'person'
    WHEN JSON_EXTRACT(json_text, '$.sme') is not null then 'sme'
  END AS cust_type
FROM UNNEST([
  '{"corp": {"address": {"city": "London","countryCode": "gb","streetAddress": ["Fairlop road"],"zip": "e111bn"},"cin": 1234567420,"title": "Demo Corp"}}',
  '{"person": {"address": {"city": "Madrid","countryCode": "es","streetAddress": ["Some street 1"],"zip": "z1123ab"},"cin": 1234567411,"title": "Demo Person"}}'
  ]) AS json_text;

您也可以使用函数提取第一级密钥,无论它们是什么。

CREATE TEMP FUNCTION json_keys(input STRING) RETURNS ARRAY<STRING> LANGUAGE js AS """
  return Object.keys(JSON.parse(input))
""";

SELECT json_keys(json_text) AS cust_type
  FROM UNNEST([
  '{"corp": {"address": {"city": "London","countryCode": "gb","streetAddress": ["Fairlop road"],"zip": "e111bn"},"cin": 1234567420,"title": "Demo Corp"}}',
  '{"person": {"address": {"city": "Madrid","countryCode": "es","streetAddress": ["Some street 1"],"zip": "z1123ab"},"cin": 1234567411,"title": "Demo Person"}}'
  ]) AS json_text;

输出: