I have a JSON structure similar to the following:
{"Name": "Value 1",
"Colors": {
{"Basic colors": {
"Primary colors": {
[{"First Color":"Blue", "Second Color": "Red"}]
}
}
}
}
Using JSON_EXTRACT(Name.Colors, '$.Basic_colors[0].Primary_colors)I can return the array of a struct, and then extract the values from the struct. However, there can be multiple items in "Primary Color" such as:
[{"First Color":"Blue", "Second Color": "Red"},{"First Color":"Green", "Second Color": "Orange"}]
Ideal solution:
Name | First Color | Second Color
Value 1 | Blue | Red
Value 1 | Green | Orange
The problem I've had is that, when using JSON_EXTRACT, "Primary Colors" isn't recognized as an array or struct. It is seen as a string (Makes sense, but also isn't able to be CAST() - so can't UNNEST().
The second problem I've run into is that, while I can index to [0] element in array while using JSON_extract, I can't loop for each element generating a new row of data with all child elements.
I can't use FNSPLIT, as it isn't supported in standard SQL. I believe I'd like to UNNEST(), but I can't figure out how to transform the STRUCT to be recognized as the first element in the array. My python-saturated brain wants to loop through: for item in range(len(json_array)):
I can't use Python for this (in production) and there must be an easier way than writing a loop in a SQL macro?