如何在逻辑应用程序中循环遍历数组?

Mar*_*ink 4 arrays json azure-logic-apps power-automate

我已设法将所有用户数据放入数组中(请参阅此处),但现在我无法循环访问数据。构建数组后,我已将其转换为 JSON,但我无法再处理 JSON 架构中定义的字段。

我在循环中唯一可以解决的问题(我使用 JSON 正文作为 For Each 循环的输入)是正文本身,而不是用户名、邮件地址等各个字段。

我是否应该更改 JSON 架构中的某些内容来解决此问题,还是有其他问题?

编辑:请在下面找到我的 JSON 架构:

   {
       "$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"
   }
Run Code Online (Sandbox Code Playgroud)

请参阅图片了解 JSON 的外观:

JSON 模式

Hur*_*hen 8

根据我的理解,您只想循环数组来获取每个项目的名称、邮件和其他一些字段。正如您在问题中提到的,您可以使用 json 正文作为 For Each 循环的输入。没关系,不需要再做什么了。请参考下面的截图:

  1. 初始化一个变量,例如 json 数据。 在此输入图像描述

  2. 然后通过“解析 JSON”操作来解析它。 在此输入图像描述

  3. 现在,将正文设置为 Foreach 循环的输入,然后使用变量并使用“Parse JSON”中的“mail”设置值。 在此输入图像描述

  4. 运行逻辑应用程序后,我们可以看到邮件字段也被循环了。您可以在“Foreach”中轻松使用“邮件”、“姓名”等字段。 在此输入图像描述 在此输入图像描述

更新:

我检查了您的 json 架构,但它似乎与您在屏幕截图中提供的 json 数据不匹配。我可以知道你是如何生成 json 架构的吗?在我这边,我只需单击“使用示例有效负载生成架构”按钮即可生成 json 架构,它将自动生成架构。 在此输入图像描述

我使用了与你结构相同的json数据样本并生成了它的schema,请参考下面的json数据和schema:

json数据:

{
    "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"
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

架构:

{
    "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"
                ]
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)