I have this JSON file json1.json:
{
"a":"val1",
"b":"val2",
"nest1": {
"z": "x",
"test": "containval"
}
}
I need to get a new json where elements whose values that contain val are removed, ANY property can have val, this gets me close:
jq 'del(.. | select(. == "val1"))' .json1.json
it produces this:
{
"b": "val2",
"nest1": {
"z": "x",
"test": "containval"
}
}
Except that it checks for values being EQUAL to val1, and I only need where the value DOES NOT CONTAIN "val".
Desired output:
{
"a":"val1",
"b":"val2",
"nest1": {
"test": "containval"
}
}
2
Answers
The solution is simple. Instead of checking for equality, use the filters
strings
andcontains()
to select and remove the values that contain"val"
(together with their associated keys):Check it online.
You were close. This should work: