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动作预处理
- 添加
Select动作:- 数据源:
body('Parse_JSON')?['body']?[0]?['records']?[0]?['attributes'] - 映射规则:
- Key:
item()?['key'] - Value:
item()?['value']
- Key:
- 数据源:
- 在Compose中组合最终负载:
{ "targetDbId": "1001", "attributes": @{body('Select')} }
步骤3:处理多元素嵌套场景
如果body或records包含多个元素,需用Apply to each循环遍历:
- 遍历
body('Parse_JSON')?['body']?[0]?['records'] - 循环内提取单条
attributes并暂存 - 用
Join动作将所有循环输出合并为数组格式,再嵌入最终Compose结果
内容的提问来源于stack exchange,提问作者Aluri Balahari
相关产品推荐
相关产品推荐

