Home > OS >  remove element from json file using jq
remove element from json file using jq

Time:10-05

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

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.

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

but this is not working, this is my json file:

{
    "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",
        }
    }
}

CodePudding user response:

Try indexing with [] instead.

del(.body[].error)

Online demo

CodePudding user response:

delete 'error' field in every child

One way to delete every "error" field in every object:

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

CodePudding user response:

This removes error properties anywhere in your document:

del(..|.error?)

Might be sufficient, might be not, depending on the real structure of your document.

  • Related