2017-09-13 2 views
0

Ich habe mein eigenes JSON Lesezeichen-Backup erstellt nach dieser Seite: http://kb.mozillazine.org/Backing_up_and_restoring_bookmarks_-_Firefox#Creating_bookmark_backupsWie man Firefox manuelle JSON Lesezeichen Sicherung mit jq parsen?

Ich werde nicht meine JSON Lesezeichen Backup-Datei hier (es ist zu groß) buchen, können Sie Ihre eigene Datei erstellen und einen Blick auf die gesamte Datei.

Dann zum Testen habe ich versucht, nur die uri aller Lesezeichen zu erhalten (später werde ich andere Daten extrahieren), aber das hat nicht funktioniert

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 von Firefox: Firefox 55.0.2 (64 Bits) mit Ubuntu 16.04 LTS

Version von jq: JQ-1.5-1-a5b5cbe

Grüße

Antwort

0

Hier ist eine Lösung mit 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 

Wenn der obige Filter ist in filter.jqdata.json und enthält die folgende Beispiel Lesezeichen-Daten:

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

dann den Befehl

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

https://stackoverflow.com/ 
+0

produziert @ Zigobs Ich habe einige Kommentare hinzugefügt, als du gefragt hast. Um eine bessere Vorstellung davon zu bekommen, was 'tostream' macht, probieren Sie es einfach aus. z.B. 'jq -Mr tostream data.json' – jq170727