gpt4 book ai didi

json - 如何使用 jq 解析 Firefox 手动 JSON 书签备份?

转载 作者:行者123 更新时间:2023-12-01 00:54:04 24 4
gpt4 key购买 nike

我根据这个页面创建了自己的 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 版本:Firefox 55.0.2(64 位)与 Ubuntu 16.04 LTS

jq 版本:jq-1.5-1-a5b5cbe

问候

最佳答案

这是一个使用 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.jq 中并且 data.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/

关于json - 如何使用 jq 解析 Firefox 手动 JSON 书签备份?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46206367/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com