I am trying to query a table in Snowflake that is built with a single field labeled "Value" and each row is a separate JSON object.
Row 1: {
"artist": {
"artistID": "artist_1",
"artistName": "Roblox"
},
"descriptors": {
"styles": [
{
"ID": "84121",
"weight": "63"
},
{
"ID": "83983",
"weight": "14"
}
],
"duration": "240509",
"productCodes": [
{
"type": "ISRC",
"value": "isrc_1"
}
]
}
Row 2: {
"artist": {
"artistID": "artist_2",
"artistName": "Minecraft"
},
"descriptors": {
"styles": [
{
"ID": "84122",
"weight": "12"
},
{
"ID": "83983",
"weight": "14"
}
],
"duration": "400001",
"productCodes": [
{
"type": "ISRC",
"value": "isrc_2"
}
]
}
Row 3: {
"artist": {
"artistID": "artist_3",
"artistName": "Fortnite"
},
"descriptors": {
"styles": [
{
"ID": "84121",
"weight": "47"
},
{
"ID": "83983",
"weight": "14"
}
],
"duration": "300001",
"productCodes": [
{
"type": "ISRC",
"value": "isrc_3"
},
{
"type": "ISRC",
"value": "isrc_4"
}
]
}
What I am trying to do is SELECT a column which includes all of the ISRCs, and another column which includes their associated styles. As you can see, some rows can have multiple ISRCs, and each row can have multiple styles. The output dataframe should look like this:
I’m having a hard time wrapping my head around the nested arrays – can you point me in the right direction? Thank you!
2
Answers
this worked for me :
Output
So using a CTE just like Ahmed:
this really could be a table, but a CTE makes no clean-up.
so that is your example data, and you are JOIN to projections from that data together with an implicit
CROSS JOIN
that is what the comma after the FROM table name does.I explain this, because you mention, your code get you null:
because if I use the SQL you put into the comment against, CTE of your data it works:
so to debug this I would first check pc is flatten’ing correctly:
if you get for your example data, 4+ row of null, you are pulling
pc.value
apart incorreclty.if you get zero rows, the data you are feeding into the flatten is wrong.
if you get four perfect rows as my picture, you can now swap to check the values getting pulled apart for
s
.another thing you can do to check what way the data is, is to cast
jd.data
totext
and the push intoparse_json
and see if it then works.