简体   繁体   English

Google Big Query - 从 JSON 字符串中提取未明确标记为键的键

[英]Google Big Query - Extract keys from JSON string that are not explicitly marked as keys

I have the following JSON string (I added line breaks for visibility, in actual code all of it is squished in one line)我有以下 JSON 字符串(我添加了换行符以提高可见性,在实际代码中,所有这些都被压缩在一行中)

    {"schema":
       {"properties":
          {"key_1":{"label":"key 1","type":"string"},
           "key_2":{"label":"key 2","type":"string"},
           "ley_3":{"label":"key 3","type":"string"},
           "key_4":{"label":"key 4","type":"string"},
           ...
          } 
       }
    }

What I am trying to do is extract all keys and labels associated with the key.我想要做的是提取与该键关联的所有键和标签。 I know how to do this when key is explicitly stated in JSON , but in this example key is not explicitly stated.我知道如何在 JSON 中明确说明密钥时执行此操作,但在此示例中未明确说明密钥。

I followed Google Big Query documentation on working with JSON strings, and here is how far I got:我遵循了关于使用 JSON 字符串的Google Big Query 文档,这是我得到的结果:

SELECT json_schema, JSON_EXTRACT(json_schema, "$.schema.properties"), JSON_EXTRACT(json_schema, "$.schema.properties[1]")
FROM schemas

json_schema is a column name in the schemas table. json_schemaschemas表中的列名。

This gets me to the right direction, but I don't know how to proceed from here.这让我朝着正确的方向前进,但我不知道如何从这里开始。 My desired output is (for example), is:我想要的 output 是(例如),是:

key    value
key_1  key 1
key_2  key 2
key_3  key 3
key_4  key 4

Here is the code to reproduce the example:这是重现示例的代码:

SELECT '{"schema":{"properties":{"key_1":{"label":"key 1","type":"string"},"key_2":{"label":"key 2","type":"string"},"key_3":{"label":"key 3","type":"string"},"key 4":{"label":"key_4","type":"string"}}}}' AS json_schema

Consider below approach考虑以下方法

select key, 
  json_extract_scalar(regexp_extract(props, r'"' || key || '":({.*?})'), '$.label') value
from your_table, 
unnest([struct(json_extract(json_schema, '$.schema.properties') as props)]),
unnest(`bqutil.fn.json_extract_keys`(props)) key 

       

if applied to sample data in your question如果应用于您问题中的样本数据

with your_table as (
   select '''
    {"schema":
       {"properties":
          {"key_1":{"label":"key 1","type":"string"},
           "key_2":{"label":"key 2","type":"string"},
           "ley_3":{"label":"key 3","type":"string"},
           "key_4":{"label":"key 4","type":"string"}
          } 
       }
    }
   ''' json_schema
)              

output is output 是

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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