'Querying/flattening multi-level nested JSON in Cosmos DB when array name is random on one level deep down

I would like to get some help to write a query to get a flat JSON to feed Power BI. The data store is a Cosmos DB container with JSON. The structure is fixed.

The example JSON stucture is this:

"orderid": "999888777",
    "name": "dwayne.johnson",
    "mainData": {
        "giftData": {
            "name": "Rocketship",
            "giftDetails": {
                "**123456789**": {
                    "boxInfo": [
                        {
                            "name": "Leonardo Da Vinci Big Box",
                            "city": "London",
                            "country": "United Kingdom",
                            "currency": "EUR",
                            "price": 99.43,
                            
                        }
                    ],
                    "colorScheme": [
                        {
                            "name": "Red Apple Delight",
                            "code": "123AAA",
                        }
                    ],
                    "CourierInfo": [
                        {
                            "name": "UPS",
                            "code": "BBBA333",
                        }
                    ],

"orderid": "444333434",
    "name": "chris.rock",
    "mainData": {
        "giftData": {
            "name": "Junglemaster XXL",
            "giftDetails": {
                "**32432423424**": {
                    "boxInfo": [
                        {
                            "name": "Gerba Tool Big O",
                            "city": "München",
                            "country": "Germany",
                            "currency": "EUR",
                            "price": 103.22,
                            
                        }
                    ],
                    "colorScheme": [
                        {
                            "name": "Green Mix",
                            "code": "999DDD",
                        }
                    ],
                    "CourierInfo": [
                        {
                            "name": "GLS",
                            "code": "CCCGV3",
                        }
                    ],

I would like to get to following output:

orderid currency* price**
999888777 EUR 99.43
444333434 EUR 103.22

*The path would be giftData.giftDetails.123456789 or 32432423424 or any random.boxInfo.currency

**The path would be giftData.giftDetails.123456789 or 32432423424 or any random.boxInfo.price

The question is how can I select the currency and price info 4 levels deep, when the container array name is a random number (in bold)?

Thank you very much for your time and help!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source