【发布时间】:2018-08-26 18:47:46
【问题描述】:
jq 应该是
处理/过滤 JSON 输入并生成过滤器的结果作为 JSON
但是,我发现在jq 处理/过滤之后,输出结果不再是JSON 格式。
例如,https://stedolan.github.io/jq/tutorial/#result5,即,
$ curl -s 'https://api.github.com/repos/stedolan/jq/commits?per_page=5' | jq '.[] | {message: .commit.message, name: .commit.committer.name}'
{
"message": "Merge pull request #162 from stedolan/utf8-fixes\n\nUtf8 fixes. Closes #161",
"name": "Stephen Dolan"
}
{
"message": "Reject all overlong UTF8 sequences.",
"name": "Stephen Dolan"
}
. . .
有什么解决方法吗?
更新:
如何将整个return包装成一个json结构:
{ "Commits": [ {...}, {...}, {...} ] }
我试过了:
jq '.[] | Commits: [{message: .commit.message, name: .commit.committer.name}]'
jq 'Commits: [.[] | {message: .commit.message, name: .commit.committer.name}]'
但两者都不起作用。
【问题讨论】:
-
这个 Q/A 帮助我找到了 better Github search approach,仅供参考。