I have a VARCHAR column storing JSON data. Here is one row:
{
"id": null,
"ci": null,
"mr": null,
"meta_data":
{
"product":
{
"product_id": "123xyz",
"sales":
{
"d_code": "UK",
"c_code": "5814"
},
"amount":
{
"currency": "USD",
"value": -1230
},
"entry_mode": "virtual",
"transaction_date": "2020-01-01",
"transaction_type": "purchase",
"others":
[]
}
}
}
Example data:
WITH t1 AS (
SELECT '{"id":null,"ci":null,"mr":null,"meta_data":{"product":{"product_id":"123xyz","sales":{"d_code":"UK","c_code":"5814"},"amount":{"currency":"USD","value":-1230},"entry_mode":"virtual","transaction_date":"2020-01-01","transaction_type":"purchase","others":[]}}}'::varchar AS value
)
In Postgres, I do like this. How can I extract the following values below in Snowflake?
SELECT
value,
value -> 'meta_data' -> 'product' ->> 'product_id' AS product_id,
value -> 'meta_data' -> 'product' -> 'sales' ->> 'd_code' AS d_code,
value -> 'meta_data' -> 'product' -> 'sales' ->> 'c_code' AS c_code,
value -> 'meta_data' -> 'product' -> 'amount' ->> 'currency' AS currency,
value -> 'meta_data' -> 'product' ->> 'entry_mode' AS entry_mode,
value -> 'meta_data' -> 'product' ->> 'transaction_type' AS transaction_type
FROM t1
