power-automate

Apply to each in array


In Power Automate, I get the following object out of a Parse JSON action:

{
    "body": {
        "order_id": "ORD-20250221-001",
        "order_date": "2025-02-21",
        "order_total": 1349.98,
        "payment_method": "Credit Card",
        "order_status": "Processing",
        "estimated_delivery_date": "2025-02-25",
        "shipping_carrier": "FastExpress",
        "tracking_number": "FAST123456789US"
    }
}

The number of items in this object is variable.

I want an array that I can loop in an apply to each and extract each key/value.

I've managed to convert the JSON into an array with a compose:

replace(replace(string(body('Parse_JSON')),'{','['),'}',']')

And:

split(outputs('Compose'),',')

enter image description here


Solution

  • Here's a free of cost solution to do this with zero computational load.

    I have started with your object and modified + merged your expression to convert it to an array. Then, we create a separator (in my case '$%#') that we know will assist in evenly splitting our data. choose a string or combination of characters that you know would never appear in the data.

    enter image description here

    Expression:

    split(replace(replace(replace(replace(replace(replace(string(variables('Main')?['body']),'{','['),'}',']'),'":"','$%#'),'"',''),']',''),'[',''),',')
    

    The compose returns an array like this

    enter image description here

    Then we use a select to generate an array of Key/value pairs. enter image description here

    the structure looks like this

    enter image description here

    upon test run, we get the desired output.

    enter image description here

        [
      {
        "Key": "order_id",
        "Value": "ORD-20250221-001"
      },
      {
        "Key": "order_date",
        "Value": "2025-02-21"
      },
      {
        "Key": "order_total:1349.98",
        "Value": "order_total:1349.98"
      },
      {
        "Key": "payment_method",
        "Value": "Credit Card"
      },
      {
        "Key": "order_status",
        "Value": "Processing"
      },
      {
        "Key": "estimated_delivery_date",
        "Value": "2025-02-25"
      },
      {
        "Key": "shipping_carrier",
        "Value": "FastExpress"
      },
      {
        "Key": "tracking_number",
        "Value": "FAST123456789US"
      }
    ]
    

    Overall flow complexity is null

    enter image description here