Mapping Nested JSON array of objects into a table

N00b here, im very new to retool and and very new to programming. So any help would be appreciated. I have a workflow that grabs order (ecommerce) info from an api, then loops and gets Order Details from the same API.

The response comes back with the "Products" from this order inside an array of key/values like this:

// this is nested inside {{query1.data['0'].data['0'].custom_field
"custom_field": [
            {
              "label": "Order_Details",
              "display_name": "Order Details",
              "data_type": "Table",
              "app_side": "1",
              "required": 0,
              "value": 1,
              "data": {
                "head": [
                  {
                    "label": "QTY",
                    "display_name": "QTY",
                    "type": "number",
                    "arth": " ",
                    "id": 0,
                    "show": 1,
                    "app_side": 0,
                    "required": 0
                  },
                  {
                    "label": "ITEM",
                    "display_name": "ITEM",
                    "type": "text",
                    "arth": " ",
                    "id": 1,
                    "show": 1,
                    "app_side": 0,
                    "required": 0
                  },
                  {
                    "label": "PRICE",
                    "display_name": "PRICE",
                    "type": "number",
                    "arth": " ",
                    "id": 2,
                    "show": 1,
                    "app_side": 0,
                    "required": 0
                  },
                  {
                    "label": "ID",
                    "display_name": "ID",
                    "type": "number",
                    "arth": " ",
                    "id": 3,
                    "show": 1,
                    "app_side": 0,
                    "required": 0
                  }
                ],
                "other": [],
                "body": [
                  [
                    {
                      "val": 1, // qty of product1 
                      "id": 0,
                      "head": "QTY",
                      "arth": " "
                    },
                    {
                      "val": "product1", // Name of Product
                      "id": 1,
                      "head": "ITEM",
                      "arth": " "
                    },
                    {
                      "val": 35, // price of product1
                      "id": 2,
                      "head": "PRICE",
                      "arth": " "
                    },
                    {
                      "val": 154, // product_id of product1
                      "id": 3,
                      "head": "ID",
                      "arth": " "
                    }
                  ],
                  [
                    {
                      "val": 1, // qty of product2
                      "id": 0,
                      "head": "QTY",
                      "arth": " "
                    },
                    {
                      "val": "Product2", // name of product2
                      "id": 1,
                      "head": "ITEM",
                      "arth": " "
                    },
                    {
                      "val": 35, // price of product2
                      "id": 2,
                      "head": "PRICE",
                      "arth": " "
                    },
                    {
                      "val": 153, // id of product2
                      "id": 3,
                      "head": "ID",
                      "arth": " "
                    }
                  ],
                  [
                    {
                      "val": 1,
                      "id": 0,
                      "head": "QTY",
                      "arth": " "
                    },
                    {
                      "val": "Product 3",
                      "id": 1,
                      "head": "ITEM",
                      "arth": " "
                    },
                    {
                      "val": 35,
                      "id": 2,
                      "head": "PRICE",
                      "arth": " "
                    },
                    {
                      "val": 730,
                      "id": 3,
                      "head": "ID",
                      "arth": " "
                    }
                  ],
                  [
                    {
                      "val": 1,
                      "id": 0,
                      "head": "QTY",
                      "arth": " "
                    },
                    {
                      "val": "Product 4",
                      "id": 1,
                      "head": "ITEM",
                      "arth": " "
                    },
                    {
                      "val": 30,
                      "id": 2,
                      "head": "PRICE",
                      "arth": " "
                    },
                    {
                      "val": 721,
                      "id": 3,
                      "head": "ID",
                      "arth": " "
                    }
                  ]
                ],

What I need to do achieve, is a loop that will create a row in the table (order_products) for each product in the invoice, which also includes other values from this query (see below).

Keep in mind.. everything that I know is self taught and im pretty (extremely) new to this.. lol Javascript still melts my brain.

order_id order_date fleet_id qty product price product_id
1234 2022-12-17 123456 1 product1 9.99 123
1234 2022-12-17 123456 1 product2 19.99 124
1234 2022-12-17 123456 1 product3 9.99 125
1235 2022-12-17 123456 1 product1 9.99 123
1235 2022-12-17 123456 1 product2 19.99 124
1235 2022-12-17 123456 1 product3 9.99 125

etc etc.

This is what I have so far - but when i run the loop it is only creating 1 row for each order and only putting the first product into the row.. I also don't know how to make it create a new line for each product in the order. (I don't really understand the [i] or {index}, but I THINK is that it represents a "wildcard" so to speak so that it knows which keys to parse through.. so.. ive tried all kinds of combinations to no avail:

{{value.data['0'].custom_field['0'].data.body[i]['0'].val}}
{{value.data['0'].custom_field['0'].data.body[index]['0'].val}}
{{value.data['0'].custom_field['0'].data.body[{index}]['0'].val}}
{{value.data['0'].custom_field['0'].data.body[{{index}}]['0'].val}}
// -----------^-----------------------------------^ 

I need it to loop through both of those keys. First one would represent each order, send key represents the products inside each order.

My apologies if I sound like a bit of a knob and using the incorrect terminology. .ive been trying to solve this issue for a week now and I keep coming back to it. Ive read countless forum posts (and im sure that one of them actually has the answers... i just cant understand it lol.

Thank you in advance for any help or insight you fine people may have.

No one to help??

Hello @cpearson4772!

This is a pretty intricate data structure, I would recommend joining our office hours on Discord where we can better help you grab the right data to put into your table.

Office hours are 11am-Noon PST Tuesday and Thursday!

It sounds like you need to do logic similar to a nested loop to iterate through the orders via value.data[i] first and then do a second loop through each product in the order later on within .data.body[j].

The i/index value will be useful for the first variable to tell the loop where to key in.

For the second inner loop I would recommend triggering another workflow, passing in the nested data of value.data[i].custom_field['0'].data.body

Then in a loop block in the second workflow you can pass in i or index on the smaller chunk of data you are sending over and iterating through.

I might not have gotten the placement right on where to put in the [i] :melting_face: but I would also recommend feeding the data structure into ChatGPT and asking it how you can loop through the order and the products within order!

This way you can break up the loops and iterations, as it is tricky to do back to back loop blocks in one single workflow.

It is much easier to loop through the array of orders in the first workflow and then pass in an array of all the products and their info within an order to another workflow, where you will just need to use i to tell each database post which array in the array you are looping through to grab data from.

You might have already seen it but this user posted in the forum about trying to do back to back loops and got a helpful answer on dividing up logic/looping.

Hope this can help! :sweat_smile: