【问题标题】:Azure Logic Apps form a new array from two array collections based on a conditionAzure 逻辑应用根据条件从两个数组集合形成一个新数组
【发布时间】:2020-03-11 10:00:00
【问题描述】:

我有两个数组集合如下

"**entitiy1**": [
{
  "lotNumber": "Lot04",
  "itemId": "1000123",
  "locationId": "65668"
},
{
  "lotNumber": "Lot02",
  "itemId": "1000123"
  "locationId": "1000006"
}]


"**entitiy2**":[
{
"storageLocationId": "1000006",
"storageLocationName": "Back Cooler"
},
{
"storageLocationId": "1000007",
"storageLocationName": "Stock Room"
}]

最终的新数组应该通过使用locationId和storageLocationId匹配数据来形成

[{
  "lotNumber": "Lot02",
  "itemId": "1000123"
  "locationId": "1000006",
  "storageLocationName":"Back Cooler"
}]

我正在尝试如下。我正在遍历一个数组并使用当前项目匹配条件过滤第二个数组。 但是对于每个循环总是显示为失败。它显示为上一个和下一个迭代链接。当我们滚动到下一个迭代时,它显示为匹配项已通过。但我只需要匹配的输出

第三行有匹配记录

如何形成一个包含所有匹配记录的最终数组?

"For_each": {
            "actions": {
                "Append_to_array_variable": {
                    "inputs": {
                        "name": "finalArray",
                        "value": "@outputs('Compose')"
                    },
                    "runAfter": {
                        "Compose": [
                            "Succeeded"
                        ]
                    },
                    "type": "AppendToArrayVariable"
                },
                "Compose": {
                    "inputs": {
                        "itemCount": "@items('For_each')?['itemCount']",
                        "locationId": "@items('For_each')?['locationId']",
                        "locationName": "@body('Filter_array')?[0]['storageLocationName']",
                        "lotNumber": "@items('For_each')?['lotNumber']",
                        "sellByDate": "@items('For_each')?['sellByDate']"
                    },
                    "runAfter": {
                        "Filter_array": [
                            "Succeeded"
                        ]
                    },
                    "type": "Compose"
                },
                "Filter_array": {
                    "inputs": {
                        "from": "@body('Select_ESO_Locations')",
                        "where": "@equals(string(item()['storageLocationId']), string(items('For_each')['locationId']))"
                    },
                    "runAfter": {},
                    "type": "Query"
                }
            },
            "foreach": "@body('Select_LSO_Locations')",
            "runAfter": {
                "Initialize_variable": [
                    "Succeeded"
                ]
            },
            "type": "Foreach"
        }

【问题讨论】:

    标签: azure-logic-apps power-automate


    【解决方案1】:

    您的撰写操作失败,因为您尝试使用硬编码索引 [0] 从过滤后的数组中读取数据。只需将您读取值的方式更改为 在 Compose 中的每个内部 代替

    "locationName": "@body('Filter_array')?[0]['storageLocationName']",

    "locationName": "@body('Filter_array')?[0]?['storageLocationName']",

    您修改后的 For Each 应如下所示

    "For_each": {
                "actions": {
                    "Append_to_array_variable": {
                        "inputs": {
                            "name": "finalArray",
                            "value": "@outputs('Compose')"
                        },
                        "runAfter": {
                            "Compose": [
                                "Succeeded"
                            ]
                        },
                        "type": "AppendToArrayVariable"
                    },
                    "Compose": {
                        "inputs": {
                            "itemCount": "@items('For_each')?['itemCount']",
                            "locationId": "@items('For_each')?['locationId']",
                            "locationName": "@body('Filter_array')?[0]?['storageLocationName']",
                            "lotNumber": "@items('For_each')?['lotNumber']",
                            "sellByDate": "@items('For_each')?['sellByDate']"
                        },
                        "runAfter": {
                            "Filter_array": [
                                "Succeeded"
                            ]
                        },
                        "type": "Compose"
                    },
                    "Filter_array": {
                        "inputs": {
                            "from": "@body('Select_ESO_Locations')",
                            "where": "@equals(string(item()['storageLocationId']), string(items('For_each')['locationId']))"
                        },
                        "runAfter": {},
                        "type": "Query"
                    }
                },
                "foreach": "@body('Select_LSO_Locations')",
                "runAfter": {
                    "Initialize_variable": [
                        "Succeeded"
                    ]
                },
                "type": "Foreach"
            }

    附言 话虽如此,请考虑以下选项 1)使用将json转换为json的液体转换。通过编写液体变换,您可以更好地控制变换。请注意,这需要一个集成帐户,如果您在企业中使用它,我强烈建议您使用此路径 2) 使用 Azure 函数进行转换

    【讨论】:

    • 液体变换是一个很有前途的探索。但是我们现在没有企业帐户。关于 Azure 函数,我们在函数中编写上述解决方案是否有任何优势,而不是像上面那样使用 for each 循环和过滤器来考虑成本/其他事情(如果有的话)?
    • 在我看来,选项 1 和 2 可以相互补充。在生产环境中配置集成帐户将消耗一些金钱资源,如果他们的组织中使用的集成数量非常少,许多人不想这样做。在这种情况下,我建议使用选项 2,我们在 Azure Functions 中编写备用映射逻辑。例如,我没有使用集成帐户来执行 xslt 转换,而是创建了可重用的 azure 函数来执行转换。同样可以使用点液体库实现 json 到 json 的转换
    猜你喜欢
    • 2011-06-21
    • 2020-09-12
    • 2019-08-11
    • 2019-05-13
    • 2020-05-13
    • 2012-03-04
    • 1970-01-01
    • 2021-05-02
    相关资源
    最近更新 更多