unstuck
unstuck

Reputation: 650

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

Upvotes: 0

Views: 42

Answers (2)

Laviza Falak Naz
Laviza Falak Naz

Reputation: 446

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

Upvotes: 1

Skin
Skin

Reputation: 11262

The easiest way is to use the Json Properties to Name Value Pair Array operation from the Advanced Data Operations connector.

https://statesolutions.com.au/json-properties-to-name-value-pair-array/

It's not free but depending on how many calls you intend on making a month, it's potentially very VERY cheap. It's well worth it, there are a whole heap of things you can do with it.

Result

This is the output from that step ...

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

... and you can loop and filter that given it's an array.

Upvotes: 1

Related Questions