如何组合来自 JSON 值的字符串,只保留字符串的一部分?

Tuy*_*ham 3 sed text-processing filter json

我有样品:

           "name": "The title of website",
           "sync_transaction_version": "1",
           "type": "url",
           "url": "https://url_of_website"
Run Code Online (Sandbox Code Playgroud)

我想获得以下输出:

"The title of website"    url_of_website
Run Code Online (Sandbox Code Playgroud)

我需要从 URL 中删除协议前缀,这样就只剩url_of_website下了(http前面没有)。问题是我不太熟悉sed阅读多行,做了一些研究到我https://unix.stackexchange.com/a/337399/256195,仍然无法产生结果。

我试图解析的有效 json 对象是Bookmarkgoogle chrome ,示例:

{
   "checksum": "9e44bb7b76d8c39c45420dd2158a4521",
   "roots": {
      "bookmark_bar": {
         "children": [ {
            "children": [ {
               "date_added": "13161269379464568",
               "id": "2046",
               "name": "The title is here",
               "sync_transaction_version": "1",
               "type": "url",
               "url": "https://the_url_is_here"
            }, {
               "date_added": "13161324436994183",
               "id": "2047",
               "meta_info": {
                  "last_visited_desktop": "13176472235950821"
               },
               "name": "The title here",
               "sync_transaction_version": "1",
               "type": "url",
               "url": "https://url_here"
            } ]
            } ]
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Kus*_*nda 8

这适用于问题中给出的 JSON 文档:

$ jq -r '.roots.bookmark_bar.children[]|.children[]|["\"\(.name)\"",.url]|@tsv' file.json
"The title is here"     https://the_url_is_here
"The title here"        https://url_here
Run Code Online (Sandbox Code Playgroud)

这将访问.children[]每个.roots.bookmark_bar.children[]数组条目的数组并创建一个字符串,该字符串根据您在问题中显示的内容进行格式化(在两条数据之间有一个制表符)。

如果双引号是没有必要的,您可以将繁琐的改变["\"\(.name)\"",.url],只是[.name,.url]

要从https://URL 中修剪掉,请使用

.url|ltrimstr("https://")
Run Code Online (Sandbox Code Playgroud)

而不仅仅是.url.