用 Jq 递归提取对象

2021-08-02 00:28:21

Algolia 驱动的 Hacker News API 返回嵌套的评论线程,如下所示:https://hn.algolia.com/api/v1/items/27941108(对于这个故事:https://news.ycombinator.com/item? id=27941108)```json{ "id": 27941108, "created_at": "2021-07-24T14:15:05.000Z", "type": "story", "author": "edward", "title" ": "Unix 域套接字的乐趣", "url": "https://simonwillison.net/2021/Jul/13/unix-domain-sockets/", "children": [ { "id": 27942287, " created_at": "2021-07-24T16:31:18.000Z", "type": "comment", "author": "DesiLurker", "text": "一个鲜为人知的......”,“孩子”:[] },{“id”:27944615,“created_at”:“2021-07-24T21:26:33.000Z”,“类型”:“评论”,“作者": "galaxyLogic", "text": "我从维基百科上读到这个...", "children": [ { "id": 27944746, "created_at": "2021-07-24T21:49:07.000Z", "type": "comment", "author" :“hughrr”,“文本”:“是的,虽然我...", "children": [] } ] } ]}```我想把它压平成一个项目数组,这样我就可以将它发送到`sqlite-utils insert`。这个配方有效:` ``curl 'https://hn.algolia.com/api/v1/items/27941108' \ | jq '[recurse(.children[]) | del(.children)]' \ | sqlite-utils 插入 hn. db items - --pk id```这里的`jq`配方是:```jq[recurse(.children[]) | del(.children)]```第一个`recurse(.children[]) ` 递归遍历 `.children` 数组中所有内容的列表。`| del(.children)` 然后从返回的对象中删除该数组。将其全部包装在 `[ ]` 中确保整体结果将是一个数组。应用上面的例子,这将返回:```json[ { "id": 27941108, "created_at": "2021-07-24T14:15:05.000Z", "type": "story", "author": "edward", "title": "Fun with Unix domain sockets", "url": "https://simonwillison.net/2021/Jul/13/unix-domain-sockets/" }, { "id": 27942287 , "created_at": "2021-07-24T16:31:18.000Z", "type": "comment", "author": "DesiLurker", "text": "一个鲜为人知的..." }, { "id": 27944615, "created_at": "2021-07-24T21:26:33.000Z", "type": "comment", "author": "galaxyLogic", "文本”: ”我从维基百科上读到这个..." }, { "id": 27944746, "created_at": "2021-07-24T21:49:07.000Z", "type": "comment", "author": "hughrr" , “文本”: ”是的,虽然我..." }]```