Copy nested items to new array

Hi,

I have the following issue:
I have this JSON input:

{
    "products": {
        "10101702": {
            "sku": "10101702",
            "warehouse_products": {
                "warehouse_1": {
                    "warehouse_id": "warehouse_1",
                    "on_hand": 0,
                    "allocated": 0,
                    "backorder": 0,
                    "available": 0,
                    "non_sellable": 0
                },
                "warehouse_12": {
                    "warehouse_id": "warehouse_12",
                    "on_hand": 0,
                    "allocated": 0,
                    "backorder": 0,
                    "available": 0,
                    "non_sellable": 0
                }
            }
        },
        "10102402": {
            "sku": "10102402",
            "warehouse_products": {
                "warehouse_1": {
                    "warehouse_id": "warehouse_1",
                    "on_hand": 0,
                    "allocated": 0,
                    "backorder": 0,
                    "available": 0,
                    "non_sellable": 0
                }
            }
        },
        "10102503": {
            "sku": "10102503",
            "warehouse_products": {
                "warehouse_1": {
                    "warehouse_id": "warehouse_1",
                    "on_hand": 0,
                    "allocated": 0,
                    "backorder": 0,
                    "available": 0,
                    "non_sellable": 0
                }
            }
        }
    }
}

I want a output like this:

{
    "Inventroy": [
        {
            "warehouse_id": "warehouse_1",
            "on_hand": 0,
            "allocated": 0,
            "backorder": 0,
            "available": 0,
            "non_sellable": 0
        },
        {
            "warehouse_id": "warehouse_2",
            "on_hand": 0,
            "allocated": 0,
            "backorder": 0,
            "available": 0,
            "non_sellable": 0
        },
        {
            "warehouse_id": "warehouse_1",
            "on_hand": 0,
            "allocated": 0,
            "backorder": 0,
            "available": 0,
            "non_sellable": 0
        },
        {
            "warehouse_id": "warehouse_1",
            "on_hand": 0,
            "allocated": 0,
            "backorder": 0,
            "available": 0,
            "non_sellable": 0
        }
    ]
}

So loop over warehouse_products in products and add all warehouse_products childeren to a array.

Does anyone know how to do this?

@Gugi ?

Hi @c.eggebeen

We can use JMESPath to achieve your objective. Give the below syntax a try.

&{products.*.warehouse_products.* | []}
2 Likes

Hi @Gugi,

Thanks, you’re amazing! Works perfectly

Kind regards,

Chris

Glad to hear that works. You can mark my reply above as the solution to your question :slight_smile:

Feel free to read the article on how to Use JMESPath to simplify your transformations for other applications of JMESPath in transformers.