【问题标题】:Concatenate multiple empty jq in a unix pipe在 unix 管道中连接多个空 jq
【发布时间】:2019-07-13 18:24:50
【问题描述】:

当我在 jq 中管道 json 时,我得到了正确的结果:

echo '{"foo":"bar"}' | jq
{
  "foo": "bar"
}

但是当我尝试再次将其通过管道传输到jq 时,

echo '{"foo":"bar"}' | jq | jq

我收到以下消息

jq - commandline JSON processor [version 1.5]
Usage: jq [options] <jq filter> [file...]

    jq is a tool for processing JSON inputs, applying the
    given filter to its JSON text inputs and producing the
    filter's results as JSON on standard output.
    The simplest filter is ., which is the identity filter,
    copying jq's input to its output unmodified (except for
    formatting).
    For more advanced filters see the jq(1) manpage ("man jq")
    and/or https://stedolan.github.io/jq

    Some of the options include:
     -c     compact instead of pretty-printed output;
     -n     use `null` as the single input value;
     -e     set the exit status code based on the output;
     -s     read (slurp) all inputs into an array; apply filter to it;
     -r     output raw strings, not JSON texts;
     -R     read raw strings, not JSON texts;
     -C     colorize JSON;
     -M     monochrome (don't colorize JSON);
     -S     sort keys of objects on output;
     --tab  use tabs for indentation;
     --arg a v  set variable $a to value <v>;
     --argjson a v  set variable $a to JSON value <v>;
     --slurpfile a f    set variable $a to an array of JSON texts read from <f>;
    See the manpage for more options.

我该如何解决这个问题?

在某些情况下,这是针对性能不是问题的某种自动化,因此它是否通过一些jq 传递并不重要

【问题讨论】:

    标签: json command-line jq


    【解决方案1】:

    我缺少.

    echo '{"foo":"bar"}' | jq . | jq .
    

    注意:虽然这样可以解决问题,但我不明白为什么。即使jq 打印到标准输出,下面的 jq 也无法从中读取。

    【讨论】:

    • 不同版本的 jq 在未指定过滤器时具有不同的“默认值”。当使用. 过滤器显式调用时,所有版本的 jq 行为一致。因此,人们认为通常最好明确指定过滤器是可以原谅的。
    猜你喜欢
    • 2011-06-12
    • 2021-12-13
    • 2016-03-26
    • 1970-01-01
    • 2021-04-19
    • 2011-12-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多