I have this JSON raw data that is injected to Snowflake via Kafka from devices. I am trying to convert some of the values to a table using Snowflake’s Dynamic Table (so basiacally I just need to fix the SELECT statement).
This is the snapshot of the JSON data, the column name is RECORD_CONTENT.
{
"enterprise": "xx",
"genericTrap": x,
"pduBerEncoded": "xxx",
"pduRawBytes": "xxxx",
"peerAddress": "xx",
"peerPort": "xx",
"securityName": "xxx",
"specificTrap": xx,
"sysUpTime": xxxx,
"variables": [
{
"oid": "column_a",
"type": "octetString",
"value": "xxx
},
{
"oid": "column_b",
"type": "integer",
"value": "xxx"
}
]
}
the expected output is this:
column_a | column_b | specific_trap
xxx | xxx | xx
For other devices which I only need to parse Variables, I have been able to do with this basic query:
SELECT
MAX(CASE WHEN f.value:oid::STRING = 'column_a' THEN f.value:value::STRING END) AS column_a,
MAX(CASE WHEN f.value:oid::STRING = 'column_b' THEN f.value:value::STRING END) AS column_b
FROM table_a,
LATERAL FLATTEN(input => RECORD_CONTENT:variables) f
GROUP BY
f.seq
However, introducing the specific trap column proves to be a bit challenging for me. Can you please help me?
Thank you in advanced.
Edit: specific trap only has 2 values, either 1 or 2. Hence, groupping by specific trap is not possible.
2
Answers
select
max(case when rc.value:oid::string=’column_a’ then rc.value:value::string end) column_a
,max(case when rc.value:oid::string=’column_b’ then rc.value:value::string end) column_b
,record_content:specificTrap::string as specific_trap
from json_data a,
lateral flatten(input=>record_content:variables) rc
group by specific_trap
You probably need to address one task at a time. First, extract the fields a table with the array variables into rows, and then transpose them, for example:
On the first step you produce an intermediate table like this:
Then, it’s easy enough to transpose the table to get the desired result.