Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jq: recursively delete all keys that match a given pattern

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)
like image 208
Kshitiz Sharma Avatar asked Mar 06 '17 20:03

Kshitiz Sharma


1 Answers

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)
like image 99
peak Avatar answered Oct 13 '22 01:10

peak