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

如何用jq实现yq中del(.body.*.error)的等效JSON字段删除操作

用jq删除JSON中body下所有子元素的error字段

之前用yq命令可实现需求:

yq -i 'del(.body.*.error)' "output.json"

切换到jq工具时,尝试delpaths([["body",".$","error"]])无效,正确的jq命令如下:

核心jq表达式

del(.body[].error)

直接修改文件的命令

如果使用jq 1.6及以上版本,支持-i(--in-place)参数直接修改文件,用法和yq类似:

jq -i 'del(.body[].error)' output.json

如果是旧版本jq,需通过临时文件重定向实现:

jq 'del(.body[].error)' output.json > temp.json && mv temp.json output.json

原始JSON

{
    "body": {
        "field1":{
            "name": "test",
            "error":"messsage empty"
        },
        "field2":{
            "name":"jose"
        },
        "field3":{
            "name":"tere",
            "error":"messsage empty 2"
        }
    }
}

期望结果

{
    "body": {
        "field1":{
            "name": "test",
        },
        "field2":{
            "name":"jose"
        },
        "field3":{
            "name":"tere",
        }
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.17 09:10:31