You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure Logic Apps中Compose处理Event Grid数组提取attributes问题求助

解决Event Grid嵌套数组中提取attributes的Compose配置方案

假设你的Event Grid原始数据结构(嵌套数组示例)如下:

{
  "body": [
    {
      "records": [
        {
          "attributes": [
            {"key": "Name", "value": "Alice"},
            {"key": "Age", "value": 30}
          ],
          "otherField": "sampleData"
        }
      ]
    }
  ]
}

步骤1:定位嵌套的attributes数组

先通过Parse JSON动作解析Event Grid原始数据,确保能直接访问嵌套节点。解析后,用以下表达式定位目标数组(根据实际嵌套层级调整索引):

body('Parse_JSON')?['body']?[0]?['records']?[0]?['attributes']

步骤2:转换attributes为目标结构

如果期望将attributes的key-value对转为扁平JSON(比如{"Name": "Alice", "Age": 30}),可按两种方式配置:

方式1:直接用Compose表达式

{
  "dbUpdateId": @{body('Parse_JSON')?['body']?[0]?['id']},
  "userAttributes": @{json(replace(replace(string(body('Parse_JSON')?['body']?[0]?['records']?[0]?['attributes']), '[{"key": "', '"'), '", "value": ', '": '))}
}

方式2:用Select动作预处理

  1. 添加Select动作:
    • 数据源:body('Parse_JSON')?['body']?[0]?['records']?[0]?['attributes']
    • 映射规则:
      • Key:item()?['key']
      • Value:item()?['value']
  2. 在Compose中组合最终负载:
{
  "targetDbId": "1001",
  "attributes": @{body('Select')}
}

步骤3:处理多元素嵌套场景

如果body或records包含多个元素,需用Apply to each循环遍历:

  1. 遍历body('Parse_JSON')?['body']?[0]?['records']
  2. 循环内提取单条attributes并暂存
  3. 用Join动作将所有循环输出合并为数组格式,再嵌入最终Compose结果

内容的提问来源于stack exchange,提问作者Aluri Balahari

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 11:08:14