简体   繁体   English

使用 jq 从 json 文件中删除元素

[英]remove element from json file using jq

Maybe you could give me a right direction using this 'jq' component, I was using 'yq' and now I have to change to 'jq', so I wanted to convert to equivalent but I couldn't也许您可以使用这个“jq”组件给我一个正确的方向,我使用的是“yq”,现在我必须更改为“jq”,所以我想转换为等效的但我不能

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

The idea is to delete 'error' field in every child, without specifying every field, only one general character, similar than equivalent in above yq.这个想法是删除每个子项中的“错误”字段,而不指定每个字段,只指定一个通用字符,类似于上面 yq 中的等效字符。

delpaths([["body",".$","error"]])

but this is not working, this is my json file:但这不起作用,这是我的 json 文件:

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

And this is my expectation:这是我的期望:

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

Try indexing with [] instead.尝试使用[]进行索引。

del(.body[].error)

Online demo在线演示

delete 'error' field in every child删除每个孩子的“错误”字段

One way to delete every "error" field in every object:删除每个 object 中每个“错误”字段的一种方法:

jq 'del(..|objects|.error)'

This removes error properties anywhere in your document:这将删除文档中任何位置的error属性:

del(..|.error?)

Might be sufficient, might be not, depending on the real structure of your document.可能足够,也可能不够,这取决于文档的真实结构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM