简体   繁体   English

我如何从 SNOWFLAKE 的 SQL 表中的 json 字段中选择某些键/值对

[英]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?有谁知道解决我的问题的方法?

* ID /// TYPE /// Name (JSON_FORMAT)/// Amount * * ID /// 类型 /// 名称 (JSON_FORMAT)/// 金额 *

  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.

相关问题 雪花:在雪花 SQL 中,我将如何从用户视图中看到 select 的单个字段并放入 JSON 格式? - Snowflake: In Snowflake SQL how would I select a single field from the users view and put into JSON format? 如何使用SQL在列的列表中的key:value对中选择一个值? - How do I select a value in a key:value pair within a list in a column using SQL? 如何从键值对表中选择数据 - How to select data from a key value pair table 如何从一定数量的具有相同主键的行中,选择某个列中具有最高值的行? - From a certain number of rows with the same primary key, how do I select the ones with the highest value in a certain column? 在 Snowflake 中应用键值对映射 SQL - Apply Mapping of Key-Value pair in Snowflake SQL 使用PHP如何从数据库表中检索某个字段值,然后将该单个字段值输入到新表中 - Using PHP how do I retrieve a certain field value from a database table and then input that single field value into a new table 从SQL表中选择一对 - select a pair from an SQL table 如何根据SQL中另一个字段的值选择一个字段? - How do I select a field based on the value of another field in SQL? MS SQL:如何从查找表中为每个外键对选择最新记录 - MS SQL: How to select the most recent record from a lookup table for each foreign key pair 将 JSON 字段转换为 mysql 中的键值对 - Convert JSON field to key value pair in mysql
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM