如果使用 select 过滤器,带有 curl 的 jq 会失败

jq with curl fails if select filter is used

我正在将 curl 输出管道传输到 jq:https://stedolan.github.io/jq/ 并且一切正常,直到我尝试使用 select 过滤器。

这个过滤器在他们的在线工具中工作得很好:https://jqplay.org/ 以及在我下载文件后的命令行实验中。

仅当我尝试将 curl 输出直接通过管道传输到 jq 时才会出现此问题。

这失败了:

i71178@SLCITS-L2222:~/next-gen/mongodb$ curl 'http://fhirtest.uhn.ca/baseDstu3/Patient?_format=json&_count=50&_pretty=false&_summary=data' | jq-linux64 --unbuffered -r -c '.link[] | select(.relation == next) | .url' | head -3
jq: error: next/0 is not defined at <top-level>, line 1:
.link[] | select(.relation == next) | .url                              
jq: 1 compile error
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  2421    0  2421    0     0   2413      0 --:--:--  0:00:01 --:--:--  2413
curl: (23) Failed writing body (1675 != 2736)
i71178@SLCITS-L2222:~/next-gen/mongodb$ 

这很好用:

i71178@SLCITS-L2222:~/next-gen/mongodb$ curl 'http://fhirtest.uhn.ca/baseDstu3/Patient?_format=json&_count=50&_pretty=false&_summary=data' | jq-linux64 --unbuffered -r -c '.link[] | .url' | head -3  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 46801    0 46801    0     0  66256      0 --:--:-- --:--:-- --:--:-- 66290
http://fhirtest.uhn.ca/baseDstu3/Patient?_count=50&_format=json&_pretty=false&_summary=data
http://fhirtest.uhn.ca/baseDstu3?_getpages=e73ba3b4-cc7e-4028-8679-b5da1f9cbdd1&_getpagesoffset=50&_count=50&_format=json&_bundletype=searchset
i71178@SLCITS-L2222:~/next-gen/mongodb$ 

对于上下文,这是通过管道传输到 select 过滤器的内容:

i71178@SLCITS-L2222:~/next-gen/mongodb$ curl 'http://fhirtest.uhn.ca/baseDstu3/Patient?_format=json&_count=50&_pretty=false&_summary=data' | jq-linux64 --unbuffered -r -c '.link[]' | head -3
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 46801    0 46801    0     0  64411      0 --:--:-- --:--:-- --:--:-- 64375
{"relation":"self","url":"http://fhirtest.uhn.ca/baseDstu3/Patient?_count=50&_format=json&_pretty=false&_summary=data"}
{"relation":"next","url":"http://fhirtest.uhn.ca/baseDstu3?_getpages=00952912-c9ab-47ca-826c-200bddffe617&_getpagesoffset=50&_count=50&_format=json&_bundletype=searchset"}
i71178@SLCITS-L2222:~/next-gen/mongodb$ 

非常感谢任何帮助。

谢谢!

问题显然是您的 select 过滤器:

 select(.relation == next)

我想你的意思是:

select(.relation == "next")

更安全的是:

select(.relation? == "next")