jsonpower-automatejsonparsersharepoint-list

JSON Object to Array with Attribute Name using Power Automate


I am trying to create columns in a SharePoint List from JSON using Power Automate flow.

I managed to create a column with a "Send an HTTP request to SharePoint" manually using : { "__metadata": { "type": "SP.Field" },"FieldTypeKind": 9,"Title":"TestColumnInt"}

However, I want to automate the process. So I want to loop the whole thing depending on the number of column needed as described by the JSON.

For the first step, I can't manage to extract the attribute name in an array. For instance, for this input (the input is coming out of a PARSE JSON flow elements) :

[
    {
        "date": "08/31/2020",
        "hour": 1,
        "production": 1,
        "machine": "Z22",
        "completeDate": "08/31/2020 01:00"
    },
    {
        "date": "08/31/2020",
        "hour": 4,
        "production": 1,
        "machine": "Z44",
        "completeDate": "08/31/2020 04:00"
    }
]

I want the output :

[ "date","hour","production", "machine","completeDate"] 

Unfortunately, I can't seem to understand how the Object variables are used. Each time I use a function, like split for instance, I receive an error similar to "String or Array expected not Object".

The function keys seems not compatible too.

Thank you in advance for your help !


Solution

  • You can use the Advanced Data Operations connector and the Json Properties to Name Value Pair Array operation to turn the first item in the array into a key/value structure that will then allow you to extract the property names.

    Note: Each item needs to be balanced (i.e. have the same set of fields) others you'll need to do more work to determine that. There are other operations within that connector that will help you achieve that though.

    Flow

    The Initialize JSON step merely contains your JSON and the next step will transform the structure into a JSON object that provides you with the ability to extract the field names dynamically.

    This is the JSON that will be returned ...

    [
      {
        "propertyName": "completeDate",
        "propertyType": "String",
        "propertyValue": "08/31/2020 01:00"
      },
      {
        "propertyName": "date",
        "propertyType": "String",
        "propertyValue": "08/31/2020"
      },
      {
        "propertyName": "hour",
        "propertyType": "Integer",
        "propertyValue": 1
      },
      {
        "propertyName": "machine",
        "propertyType": "String",
        "propertyValue": "Z22"
      },
      {
        "propertyName": "production",
        "propertyType": "Integer",
        "propertyValue": 1
      }
    ]
    

    You can then loop over that array and extract each property name and create the array as you've described in your question.