Daniel Daniel - 2 months ago 25
JSON Question

JQ: how to delete object from array based on regex filter?

I have the following JSON array (simplified example):

[ {
"op": "remove",
"path": "/cache"
},
{
"op": "add",
"path": "/game_config"
},
{
"op": "remove",
"path": "/use_replication"
},
{
"op": "add",
"path": "/copy_replication"
}
]


I want to remove from the array the objects that contain "replication". The result would be:

[ {
"op": "remove",
"path": "/cache"
},
{
"op": "add",
"path": "/game_config"
}
]


I've found a way to get only them:

.[] | select( .path | contains("replication"))


However I could not find a way to remove only them from my array, keeping all the other objects.

Answer

Negation can be achieved using not. In your case, since you mentioned using regular expressions, you could use test/1 as follows:

map( select(.path | test("replication") | not))