我似乎无法jq
在shell管道中正常运行。例如:
$ curl -s https://api.github.com/users/octocat/repos | jq | cat
结果jq
只是打印出帮助文本*。如果我尝试将jq
输出重定向到文件,会发生同样的情况:
$ curl -s https://api.github.com/users/octocat/repos | jq > /tmp/stuff.json
我怎样才能防止这种行为,以便我可以jq
在管道中使用?
发布于 2018-03-02 13:16:19
你需要提供一个过滤器作为参数。要通过jq
默认情况下未经修改的JSON来传递JSON ,请使用标识过滤器.
:
curl -s https://api.github.com/users/octocat/repos | jq '.' | cat
发布于 2018-03-02 13:58:43
我发现自己经常做的一个用例是“如何构建JSON数据以供应给其他shell命令,例如curl
?” 我这样做的方式是使用--null-input/-n
选项:
根本不读任何输入!相反,过滤器运行一次使用
null
作为输入。当jq
用作简单计算器或从头开始构建JSON数据时,这非常有用。
还有一个例子将它传递给curl
:
jq -n '{key: "value"}' | curl -d @- \
--url 'https://some.url.com' \
-H 'Content-Type: application/json' \
-H 'Accept: application/json'
https://stackoverflow.com/questions/-100007491
复制相似问题