0

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.

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.

I'm new to SnowSQL and i can't seems to find a way to extract this within a regular query. Does anyone knows a way around my problem ?

* ID /// TYPE /// Name (JSON_FORMAT)/// Amount *

  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.

Thank you !

2 Answers2

0

Almost any time you use JSON in Snowflake, it's advisable to use the VARIANT data type. You can use the parse_json function to convert a string into a variant with 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. The second column uses the variant, extracting the "En" property and casting it to a string data type.

You can define columns as variant and store JSON natively. That's going to improve performance and allow parsing using dot notation in SQL.

Greg Pavlik
  • 10,089
  • 2
  • 12
  • 29
0

For anyone else who also stumbles upon this question:

You can also use JSON_EXTRACT_PATH_TEXT. Here is an example, if you wanted to create a new column called meal.

    select json_extract_path_text(Name,'En') as meal from ...