[英]how do i select certain key/value pair from json field inside a SQL table in SNOWFLAKE
I am currently working on building a dataware house in snowflake for the business that i work for and i have encounter some problems.我目前正在为我工作的企业在雪花中构建一个数据仓库,我遇到了一些问题。 I used to apply the function Json_value in TSQL for extracting certain key/value pair from json format field inside my original MSSQL DB.
我曾经在 TSQL 中应用函数 Json_value 从我原来的 MSSQL 数据库中的 json 格式字段中提取某些键/值对。
All the other field are in the regular SQL format but there is this one field that i really need that is formated in JSON and i can't seems to exact the key/value pair that i need.所有其他字段都采用常规 SQL 格式,但有一个字段是我真正需要的,它采用 JSON 格式,我似乎无法确定我需要的键/值对。
I'm new to SnowSQL and i can't seems to find a way to extract this within a regular query.我是 SnowSQL 的新手,我似乎找不到在常规查询中提取它的方法。 Does anyone knows a way around my problem?
有谁知道解决我的问题的方法?
1 5 {En: "lunch, fr: "diner"} 10.00
I would like to extract this line (for exemple) and be able to only retrieve the EN: "lunch" part from my JSON format field.我想提取这一行(例如)并且只能从我的 JSON 格式字段中检索 EN: "lunch" 部分。
Thank you !谢谢 !
Almost any time you use JSON in Snowflake, it's advisable to use the VARIANT data type.几乎任何时候在 Snowflake 中使用 JSON 时,都建议使用 VARIANT 数据类型。 You can use the parse_json function to convert a string into a variant with JSON.
您可以使用 parse_json function 将字符串转换为带有 JSON 的变体。
select
parse_json('{En: "lunch", fr: "diner"}') as VARIANT_COLUMN,
VARIANT_COLUMN:En::string as ENGLISH_WORD;
In this sample, the first column converts your JSON into a variant named VARIANT_COLUMN.在此示例中,第一列将您的 JSON 转换为名为 VARIANT_COLUMN 的变体。 The second column uses the variant, extracting the "En" property and casting it to a string data type.
第二列使用变体,提取“En”属性并将其转换为字符串数据类型。
You can define columns as variant and store JSON natively.您可以将列定义为变量并本地存储 JSON。 That's going to improve performance and allow parsing using dot notation in SQL.
这将提高性能并允许在 SQL 中使用点符号进行解析。
For anyone else who also stumbles upon this question:对于也偶然发现这个问题的其他人:
You can also use JSON_EXTRACT_PATH_TEXT.您还可以使用 JSON_EXTRACT_PATH_TEXT。 Here is an example, if you wanted to create a new column called meal.
这是一个示例,如果您想创建一个名为 meal 的新列。
select json_extract_path_text(Name,'En') as meal from ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.