Reputation: 18627
How to recursively delete all keys that match a given pattern?
I have following jq config, but it doesn't seem to work:
walk( if (type == "object" and (.[] | test('.*'))) then del(.) else . end)
Upvotes: 13
Views: 4148
Reputation: 116880
A robust way (with respect to different jq versions) to delete all keys matching a pattern (say PATTERN) would be to use the idiom:
with_entries(select( .key | test(PATTERN) | not))
Plugging this into walk/1
yields:
walk(if type == "object" then with_entries(select(.key | test(PATTERN) | not)) else . end)
Upvotes: 23