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

如何用jq修改JSON数组中指定元素(未知索引)

使用jq修改JSON数组中指定元素的方法

直接利用jq的map函数结合条件判断就能实现,不需要知道目标元素的索引位置:

基础命令

jq '.run_list |= map(if . == "role[test09]" then "role[hello]" else . end)' input.json

命令说明

  • .run_list |= ...:定位到run_list字段,并用后续处理结果更新该字段
  • map(...):遍历数组中的每一个元素
  • 条件逻辑:当元素等于"role[test09]"时替换为"role[hello]",其余元素保持原样

原地修改原文件

如果需要直接修改输入文件,添加-i参数即可:

jq -i '.run_list |= map(if . == "role[test09]" then "role[hello]" else . end)' input.json

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 00:22:01