Zigobs
Zigobs

Reputation: 169

How to parse Firefox manual JSON bookmarks backup using jq?

I've created my own JSON bookmark backup according to this page : http://kb.mozillazine.org/Backing_up_and_restoring_bookmarks_-_Firefox#Creating_bookmark_backups

I won't post my JSON bookmark backup file here (it's too big), you can create your own file and have a look at the entire file.

Then for testing I just tried to get the uri of all bookmarks (later I will extract other datas too) but this didn't work

jq -r '.[] | .uri' bookmarks-2017-09-13.json 
jq: error (at bookmarks-2017-09-13.json:1): Cannot index string with string "uri"

jq -r '.uri' bookmarks-2017-09-13.json 
null

Version of Firefox : Firefox 55.0.2 (64 bits) with Ubuntu 16.04 LTS

Version of jq : jq-1.5-1-a5b5cbe

Regards

Upvotes: 3

Views: 1211

Answers (1)

jq170727
jq170727

Reputation: 14625

Here is a solution using tostream:

    tostream                       # read [[path],value] and [[path]] stream
  | select(length==2) as [$p,$v]   # put [path] in $p and value in $v
  | select($p[-1] == "uri")        # keep paths ending in "uri"
  | $v                             # emit value

If the above filter is in filter.jq and data.json contains the following sample bookmark data:

{
  "guid": "root________",
  "title": "",
  "index": 0,
  "dateAdded": 1000000000000000,
  "lastModified": 1000000000000000,
  "id": 1,
  "type": "text/x-moz-place-container",
  "root": "placesRoot",
  "children": [
    {
      "guid": "menu________",
      "title": "Bookmarks Menu",
      "index": 0,
      "dateAdded": 1000000000000000,
      "lastModified": 1000000000000000,
      "id": 2,
      "type": "text/x-moz-place-container",
      "root": "bookmarksMenuFolder",
      "children": [
        {
          "guid": "ygE5SOG8IWid",
          "title": "Stack Overflow",
          "index": 0,
          "dateAdded": 1000000000000000,
          "lastModified": 1000000000000000,
          "id": 3,
          "iconuri": "https://cdn.sstatic.net/Sites/stackoverflow/img/favicon.ico?v=4f32ecc8f43d",
          "annos": [
            {
              "name": "bookmarkProperties/description",
              "flags": 0,
              "expires": 4,
              "value": "Stack Overflow is the largest, most trusted online community for developers to learn, share​ ​their programming ​knowledge, and build their careers."
            }
          ],
          "type": "text/x-moz-place",
          "uri": "https://stackoverflow.com/"
        }
      ]
    }
  ]
}

Then the command

$ jq -Mr -f filter.jq data.json

produces

https://stackoverflow.com/

Upvotes: 3

Related Questions