I have a Snowflake table with one column called “Data” where each record contains JSON objects. I need help extracting specific values from nested arrays within these JSON structures.
Record 1: {
"musician": {
"id": "band_001",
"name": "ThunderCats"
},
"metadata": {
"genres": [
{
"genreID": "90201",
"priority": "85"
},
{
"genreID": "90155",
"priority": "22"
}
],
"length": "195000",
"trackCodes": [
{
"codeType": "UPC",
"code": "upc_001"
}
]
}
}
Record 2: {
"musician": {
"id": "band_002",
"name": "SkyWalkers"
},
"metadata": {
"genres": [
{
"genreID": "90202",
"priority": "76"
},
{
"genreID": "90155",
"priority": "18"
}
],
"length": "220000",
"trackCodes": [
{
"codeType": "UPC",
"code": "upc_002"
},
{
"codeType": "UPC",
"code": "upc_003"
}
]
}
}
My goal is to create a query that returns two columns: one with all UPC codes and another with their corresponding genres. Each record might have multiple codes and genres. I want the final result to show each code-genre combination as separate rows. How can I flatten these nested JSON arrays properly in Snowflake?