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

Power Automate云流中解析可变长度JSON对象的问题求助

问题描述

我有一个传入流的JSON数组(Week_JSON_Compose),除AllowanceDetails外其余内容均可正常解析,问题源于其长度动态变化。

可以正常访问单个元素:

outputs('Week_JSON_Compose')[2]['AllowanceDetails'][0]['Text']

但尝试动态拼接所有Text值时:

join(outputs('Week_JSON_Compose')[2]['AllowanceDetails']['Text'], ', ')

出现错误:

"Unable to process template language expressions in action 'Email_HTML_Compose' inputs at line '0' and column '0': 'The template language expression 'join(outputs('Week_JSON_Compose')[2]['AllowanceDetails']['Text'], ', ')' cannot be evaluated because property 'Text' cannot be selected. Array elements can only be selected using an integer index"

JSON数组示例:

[{"AllowanceDetails":[],"Date":"2024-09-09","Index":1,"Location":"Dummy Location 1","Note":"Dummy note 1: Something happened on this day.","ShiftType":"Dummy Shift Type 1"},{"AllowanceDetails":[],"Date":"2024-09-10","Index":2,"Location":"Dummy Location 2","Note":"Dummy note 2: Another event occurred here.","ShiftType":"Dummy Shift Type 2"},{"AllowanceDetails":[{"Text":"Overnight Tax Free"},{"Text":"Breakfast"},{"Text":"Evening Meal"}],"Date":"2024-09-11","Index":3,"Location":"Dummy Location 3","Note":"Dummy note 3: A training session took place.","ShiftType":"Dummy Shift Type 3"},{"AllowanceDetails":[{"Text":"Overnight Tax Free"},{"Text":"Evening Meal"}],"Date":"2024-09-12","Index":4,"Location":"Dummy Location 4","Note":"Dummy note 4: Extra security measures were introduced.","ShiftType":"Dummy Shift Type 4"},{"AllowanceDetails":[{"Text":"Overnight Tax Free"},{"Text":"Breakfast"},{"Text":"Evening Meal"}],"Date":"2024-09-13","Index":5,"Location":"Dummy Location 5","Note":"Dummy note 5: Power outage for an hour.","ShiftType":"Dummy Shift Type 5"},{"AllowanceDetails":[{"Text":"Overnight Tax Free"},{"Text":"Breakfast"},{"Text":"Evening Meal"}],"Date":"2024-09-14","Index":6,"Location":"Dummy Location 6","Note":"Dummy note 6: Smooth shift with no issues.","ShiftType":"Dummy Shift Type 6"},{"AllowanceDetails":[{"Text":"Overnight Tax Free"},{"Text":"Breakfast"},{"Text":"Evening Meal"}],"Date":"2024-09-15","Index":7,"Location":"Dummy Location 7","Note":"Dummy note 7: Testing of new equipment was done.","ShiftType":"Dummy Shift Type 7"}]
解决方案

错误原因

AllowanceDetails是数组类型,不能直接通过['Text']批量提取所有元素的Text属性,必须先遍历数组逐个提取,再进行拼接。

正确表达式

使用map函数遍历AllowanceDetails数组,提取每个元素的Text值,再用join拼接:

join(map(outputs('Week_JSON_Compose')[2]['AllowanceDetails'], item()['Text']), ', ')
  • map(数组, 处理逻辑):遍历数组中的每个元素,执行item()['Text']提取当前元素的Text值,生成一个仅包含Text内容的新数组。
  • join(数组, 分隔符):将新数组中的元素用, 拼接成字符串。

处理空数组场景

如果AllowanceDetails为空数组,上述表达式会返回空字符串。若需要显示默认文本,可添加empty判断:

if(empty(outputs('Week_JSON_Compose')[2]['AllowanceDetails']), '无补贴', join(map(outputs('Week_JSON_Compose')[2]['AllowanceDetails'], item()['Text']), ', '))

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.17 22:24:52