power-automate

Selecting all array indices with certain objects each for creating a CSV Table in Power Automate


I want to make a csv table from an array variable var_SendJson, which is a result of appending several rows of a JSON output which matched a personal criteria. The shape of the resulting array looks something like this:

[
  {
    "ID": 0
    "Title": "abc123"
    "Type": {
      "Id": 0
      "Value": "Type A"
    }
  },
  {
    "ID": 3
    "Title": "abc123"
    "Type": {
      "Id": 1
      "Value": "Type B"
    }
  },
  ...
]

I want to construct a csv table that looks like:

Title,   Type
abc123,  Type A
abc123,  Type B

Which can then be easily converted into an Excel file.

Image of the current Create CSV Block I've tried using

How do I append the variables here so that I get all the values of var_SendJson[# all values instead of only index 0]['Title'] for all the rows?

Thank you in advance to anyone who is reading this!

- GP


Solution

  • You need to manually define your fields in the CSV step ...

    Steps

    This is the code view of the Create CSV table step ...

    {
      "type": "Table",
      "inputs": {
        "from": "@variables('Array Data')",
        "format": "CSV",
        "columns": [
          {
            "header": "Title",
            "value": "@item()['Title']"
          },
          {
            "header": "Type",
            "value": "@item()['Type']['Value']"
          }
        ]
      },
      "runAfter": {
        "Initialize_Array_Data": [
          "Succeeded"
        ]
      }
    }
    

    I defined two fields with the following expressions ...

    Title = item()['Title']

    Type = item()['Type']['Value']

    Create CSV Table

    Using item() is an automatic way to reference each item in the array that you're creating the CSV table from.

    If you find that properties don’t exist at times, you can use the ? notation to stop it from erroring when referring to the property, e.g.

    item()?['Title']