Vu Le Anh Vu Le Anh - 2 months ago 33
JSON Question

JQ delete multiple properties

I have an object like this

{
"a" : 1,
"b" : {
"c" : {
"c1" : "abc",
"source" : "abcxyz"
},
"d" : {
"d1" : "abcd",
"source" : "abcxyz"
},
e" : {
"e1" : "abcde",
"source" : "abcxyz"
}
}
}


My expectation is

{
"a" : 1,
"b" : {
"c" : {
"c1" : "abc"
},
"d" : {
"d1" : "abcd"
},
e" : {
"e1" : "abcde"
}
}
}


I want to remove "source" properties. How can I do that without specifying keys "c", "d" or "e", because they are dynamic.

Thank you in advance

Answer Source

Iterate through all the elements in .b, then set their value to the result of removing the .source element from them:

.b[] |= del(.source)