How to loop through an array in a logic app?

只谈情不闲聊 提交于 2020-12-12 10:09:09

问题


I have managed to get all my userdata in an array (see here) but now I cannot loop through the data. After building the array I have converted it to JSON, but I can no longer address the fields as defined in my JSON schema.

The only thing I can address in my loop (I use the JSON body as input for the For Each loop) is the body itself, not the individual fields like username, mail address etc.

Should I change something in my JSON schema to overcome this or is something else wrong?

Edit: Please find my JSON schema below:

   {
       "$schema": "http://json-schema.org/draft-04/schema#",
       "items": [
           {
               "properties": {
                   "@@odata.type": {
                       "type": "string"
                   },
                   "createdDateTime": {
                       "type": "string"
                   },
                   "employeeId": {
                       "type": "string"
                   },
                   "givenName": {
                       "type": "string"
                   },
                   "id": {
                       "type": "string"
                   },
                   "mail": {
                       "type": "string"
                   },
                   "onPremisesSamAccountName": {
                       "type": "string"
                   },
                   "surname": {
                       "type": "string"
                   },
                   "userPrincipalName": {
                       "type": "string"
                   }
               },
               "required": [
                   "@@odata.type",
                   "id",
                   "givenName",
                   "surname",
                   "userPrincipalName",
                   "mail",
                   "onPremisesSamAccountName",
                   "employeeId",
                   "createdDateTime"
               ],
               "type": "object"
           }
       ],
       "type": "array"
   }

Please see the image for how the JSON looks:

JSON Schema


回答1:


Per my understanding, you just want to loop your array to get each item's name, mail and some other fields. As you mentioned in your question, you can use the json body as input for the For Each loop. It's ok, ther is not need to to anything more. Please refer to the screenshot below:

  1. Initialize a variable like your json data.

  2. Then parse it by "Parse JSON" action.

  3. Now, set the body as input for the For each loop, and then use a variable and set the value with "mail" from "Parse JSON".

  4. After running the logic app, we can see the mail field is also looped. You can use the "mail", "name" and other fields easily in your "For each".

Update:

I checked your json schema, but it seems can't match the json data you provided in your screenshot. May I know how did you generate your json schema, in my side I generate the json schema just by clicking the "Use sample payload to generate schema" button and it will generate the schema automatically.

I use a json data sample with the same structure of yours' and generate its schema, please refer to the json data and schema below:

json data:

{
    "body": [
        {
            "@odata.type": "test",
            "id": "123456",
            "givenName": "test",
            "username": "test",
            "userPrincipalName": "test",
            "mail": "test@mail.com",
            "onPremisesSamAccountName": "test",
            "employeeId": "test",
            "createdDateTime": "testdate"
        },
        {
            "@odata.type": "test",
            "id": "123456",
            "givenName": "test",
            "username": "test",
            "userPrincipalName": "test",
            "mail": "test@mail.com",
            "onPremisesSamAccountName": "test",
            "employeeId": "test",
            "createdDateTime": "testdate"
        }
    ]
}

schema:

{
    "type": "object",
    "properties": {
        "body": {
            "type": "array",
            "items": {
                "type": "object",
                "properties": {
                    "@@odata.type": {
                        "type": "string"
                    },
                    "id": {
                        "type": "string"
                    },
                    "givenName": {
                        "type": "string"
                    },
                    "username": {
                        "type": "string"
                    },
                    "userPrincipalName": {
                        "type": "string"
                    },
                    "mail": {
                        "type": "string"
                    },
                    "onPremisesSamAccountName": {
                        "type": "string"
                    },
                    "employeeId": {
                        "type": "string"
                    },
                    "createdDateTime": {
                        "type": "string"
                    }
                },
                "required": [
                    "@@odata.type",
                    "id",
                    "givenName",
                    "username",
                    "userPrincipalName",
                    "mail",
                    "onPremisesSamAccountName",
                    "employeeId",
                    "createdDateTime"
                ]
            }
        }
    }
}


来源:https://stackoverflow.com/questions/59324036/how-to-loop-through-an-array-in-a-logic-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!