11

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)
peak
  • 105,803
  • 17
  • 152
  • 177
Kshitiz Sharma
  • 17,947
  • 26
  • 98
  • 169

1 Answers1

19

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)
peak
  • 105,803
  • 17
  • 152
  • 177