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

Power Automate中用contains检测JSON集合日期及公式报错求助

Power Automate 检查JSON集合指定日期存在性及公式问题排查

核心需求

在Power Automate中实现:检查JSON集合内是否存在指定日期,存在返回true,不存在返回false;同时排查以下两个失效公式的问题:

公式1

if(
contains(items('Apply_to_each'), 'startDate'),
substring(items('Apply_to_each')['startDate'],0,10),
null
)

公式2

if(empty(items('Apply_to_each')?['startDate']), null, substring(items('Apply_to_each')?['startDate'],0,10))

问题分析

  1. 公式1的错误点:contains函数不支持直接检查JSON对象是否包含某个键。contains仅用于两种场景:字符串包含子串、数组包含指定元素,而items('Apply_to_each')是JSON对象,用它判断键存在性完全错误。
  2. 整体逻辑偏离需求:两个公式都只是对startDate字段做日期格式化截取,完全没有实现「检查指定日期是否存在」的核心需求,逻辑方向错误。

正确实现方案

要完成指定日期存在性检查,需结合循环与变量实现:

  • 初始化布尔变量isDateExists,默认值设为false。
  • 添加Apply to each循环,遍历目标JSON集合。
  • 循环内添加Condition动作,判断当前项的日期是否匹配目标日期:
    表达式:equals(substring(items('Apply_to_each')?['startDate'], 0, 10), '2024-05-20')(替换2024-05-20为你要检查的日期,格式需与截取后一致)
  • 条件成立时,设置isDateExists为true,并添加Terminate动作终止循环(减少无效遍历)。
  • 循环结束后,isDateExists的值即为最终判断结果。

原公式的修正(仅针对日期格式化)

如果你的原公式只是想实现「存在startDate则截取日期部分,否则返回null」,修正公式1的键存在性判断即可:

if(
not(empty(items('Apply_to_each')?['startDate'])),
substring(items('Apply_to_each')?['startDate'],0,10),
null
)

该公式与公式2逻辑一致,均可正常实现日期格式化需求。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 13:57:48