Kshitiz Sharma
Kshitiz Sharma

Reputation: 18627

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)

Upvotes: 13

Views: 4148

Answers (1)

peak
peak

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

Related Questions