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

如何在Power Automate Flow中保留JSON格式更新Azure DevOps仓库文件

解决Power Automate更新Azure DevOps JSON文件时的格式丢失问题

在Power Automate中更新Azure DevOps仓库的JSON文件时,string()函数转换后的JSON会是压缩格式,导致回写后丢失原有的换行和缩进。以下是两种可行的解决方法:

方法一:优化Replace函数组合

调整你之前的Replace逻辑,适配多层嵌套的缩进和换行需求。在Power Automate的Compose组件中使用以下表达式(注意编辑器内的换行是实际换行,而非字符串\n):

replace(
  replace(
    replace(
      replace(
        replace(
          string(variables('jsonobject')),
          '{', '{
  '
        ),
        '},', '},
'
      ),
      ',"', ',
  "'
    ),
    '}', '
}'
  ),
  '":{', '":{
    '
)

该表达式的作用:

  • 在大括号{后添加换行+2空格缩进
  • 在},后添加换行,分隔同级对象
  • 在, "后添加换行+2空格缩进,分隔同级键值对
  • 在":{(嵌套对象的起始)后添加换行+4空格缩进,适配多层结构

方法二:手动拼接格式化JSON(适合结构固定的场景)

如果你的JSON结构相对固定,直接用concat函数拼接带换行和缩进的字符串,精准控制格式:

concat(
  '{',
  decodeUriComponent('%0A'),
  '  "name": "', variables('itemName'), '",',
  decodeUriComponent('%0A'),
  '  "version": "', variables('itemVersion'), '",',
  decodeUriComponent('%0A'),
  '  "settings": {',
  decodeUriComponent('%0A'),
  '    "enabled": ', string(variables('isEnabled')), ',',
  decodeUriComponent('%0A'),
  '    "timeout": ', string(variables('timeoutValue')),
  decodeUriComponent('%0A'),
  '  }',
  decodeUriComponent('%0A'),
  '}'
)

这里用decodeUriComponent('%0A')表示换行,缩进用空格手动控制,适合不需要动态多层嵌套的JSON场景。

注意事项

  • 调用Azure DevOps的Update File API时,确保content字段传入的是格式化后的字符串,Power Automate会自动处理请求体的编码,无需额外转义。
  • 如果JSON包含特殊字符,先使用encodeUriComponent()处理对应内容后再拼接,避免格式错误。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.17 17:48:14