如何使用jq解析Firefox手册JSON书签备份?

时间:2017-09-13 20:32:34

标签: json firefox jq

我已根据此页面创建了自己的JSON书签备份: http://kb.mozillazine.org/Backing_up_and_restoring_bookmarks_-_Firefox#Creating_bookmark_backups

我不会在这里发布我的JSON书签备份文件(它太大了),您可以创建自己的文件并查看整个文件。

然后为了测试我只是试图得到所有书签的uri(后来我也将提取其他数据)但这不起作用

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

Firefox版本:使用Ubuntu 16.04 LTS的Firefox 55.0.2(64位)

jq版本:jq-1.5-1-a5b5cbe

此致

1 个答案:

答案 0 :(得分:1)

以下是使用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

如果上述过滤条件位于filter.jqdata.json包含以下示例书签数据:

{
  "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/"
        }
      ]
    }

然后是命令

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

产生

https://stackoverflow.com/