Search code examples
jsonjqvivaldi

Remove all objects containing a specific value from a JSON file using JQ?


The bookmarks file for the Vivaldi browser (based on Chromium) tends to accumulate a huge number of base64-encoded thumbnails taking up a lot of space, and I would like to remove these entries. The file is a JSON file and an entry looks like this:

{
  "date_added": "13215828073144281",
  "guid": "3ace3174-ea60-42c5-88cf-e535a150ae38",
  "id": "74",
  "meta_info": {
     "Thumbnail": "data:image/jpeg;base64,/9j/4AAQSkZJRgA....AUpSgFKUoBSlKA//2Q=="
  },
  "name": "RIPE WHOIS IP Address Database Search › Look up an IP addres… - iTools",
  "type": "url",
  "url": "http://itools.com/tool/ripe-whois-ip-address"
},

I already have a jq filter looking like this:

jq 'walk(if type == "object" then with_entries(select(.key | test("Thumbnail") | not)) else . end)' Bookmarks > Bookmarks2

The problem is this also deletes entries containing custom thumbnails like this:

"Thumbnail": "chrome://vivaldi-data/local-image/aa0d8713-99c6-4fcb-a725-a29235c4e8b0",

So the question is, how would I remove only the Thumbnail entries containing or starting with the string data:image?


Solution

  • Something like this should do the trick:

    del(recurse | objects | select(has("Thumbnail")) .Thumbnail | select(startswith("data:image")))