Thanks, but I haven't really done much in jq before. Doing the above script involved looking a lot through the manual and experimenting. This exercise was a learning experience for me. I was only able to do this because the manpage is well written.
Huh, that's the spirit.
And that's how your skills gradually become savage. Kudos.