Here is the original JSON structure:
{
"tt": [{
"box": {
"type": "A"
},
"rr": {...}
}, {
"box": {
"type": "B"
},
"rr": {...}
}, {
"box": {
"type": "C"
},
"rr": {...}
}]
}
Then I use the SQL command below to get the tt structure
with t1 as (
SELECT
rate_card,
JSON_EXTRACT_ARRAY(rate_card, '$.tt') as rr,
FROM `original_table_temp`
)
The rr JSON structure from the previous JSON structure above looks something like below
{
"rr": {
"10": { <-------------------------
"tils": {
"mdd": {
"df": {
"tif": {
"sc": 17.85, <------------------
"evr": [
{
"p": 16.35, <---------------------
"t": null,
"nr?": false
}
]
}
}
},
"11": {...},
"12": {...},
...
}
}
All the SQL commands above are executed in BigQuery.
I am interested to get the box.type, rr's keys, which are 10, 11, 12 & so on in rr into a column.
The columns next to the keys are sc & p
In the end the output of the table would be as below:
+------------+---------+---------------+
| box.type| rr | sc | p |
+------------+-------------------------+
| A | 10 | 17.85 | 16.35 |
| A | 11 |some value| .... |
......
| C | 12 | ..... | ......|
+------------+----------------------------+
At the moment, I managed to get the sc value by hard-cording in SQL BigQuery. See below:
SELECT
JSON_EXTRACT_SCALAR(rr_2, '$.rr.10.tils.mdd.df.tif.sc') AS sc,
FROM t1,
UNNEST(rr) rr_2
The method above is not productive & efficient as there are many keys like 10, 11, 12 and so on.
- How to extract the dynamic
keysfromrrinto a columns? - How to not hard-code the SQL to get
sc&p?
Feel free to remove the <---------- in the JSON sample when you test your answer.
It is a long post. If you need more information, let me know.
Thanks for your patience & time.
