【问题标题】:jq with curl fails if select filter is used如果使用选择过滤器,带有 curl 的 jq 将失败
【发布时间】:2016-09-12 21:49:45
【问题描述】:

我正在将 curl 输出传送到 jq:https://stedolan.github.io/jq/,在我尝试使用选择过滤器之前一切正常。

这个过滤器在他们的在线工具中运行良好: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$ 

对于上下文,这是进入选择过滤器的管道:

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$ 

我真的很感激这里的任何帮助。

谢谢!

【问题讨论】:

    标签: curl jq


    【解决方案1】:

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

     select(.relation == next)
    

    我想你的意思是:

    select(.relation == "next")
    

    更安全的是:

    select(.relation? == "next")
    

    【讨论】:

    • 其实,我一开始就是这样,我很确定。我似乎错误地发布了我试图让它发挥作用的尝试之一。我会仔细检查并进行修改。
    • 看来是我的复制粘贴错误。我确实试图在命令行上执行 select(.relation == next) 并且可以理解地导致了错误。很抱歉误报。
    猜你喜欢
    • 1970-01-01
    • 2021-04-09
    • 1970-01-01
    • 2020-10-09
    • 1970-01-01
    • 2018-03-13
    • 1970-01-01
    • 1970-01-01
    • 2018-09-30
    相关资源
    最近更新 更多