为何jq过滤器'exp as $var | .'未按文档输出原始输入?
关于jq变量绑定运算符的疑问
官方文档说明
jq官方文档指出(重点标注):
表达式
exp as $x | ...的含义是:针对表达式exp的每个值,使用完整的原始输入执行管道剩余部分,并将$x设为该值。
我的疑问与测试示例
我原本理解:在exp as $x | ...结构中,...的执行上下文是以jq命令的原始输入为起点。但实际测试时,exp as $x | .并没有只输出一次原始输入,而是重复输出了多次,以下是具体测试内容:
使用的过滤器
(.signal[] | select(.name | contains("signal"))) as $matches | .
输入JSON
{ "noise1": 5, "signal": [ { "name": "child-signal-1", "nested": { "prop": "child-prop-3" } }, { "name": "child-noise-2", "nested": { "prop": "child-prop-3" } }, { "name": "child-signal-3", "nested": { "prop": "child-prop-3" } } ], "noise2": [ { "name": "child-signal-1", "nested": { "prop": "child-prop-3" } }, { "name": "child-noise-2", "nested": { "prop": "child-prop-3" } }, { "name": "child-signal-3", "nested": { "prop": "child-prop-3" } } ] }
实际输出结果
{ "noise1": 5, "signal": [ { "name": "child-signal-1", "nested": { "prop": "child-prop-3" } }, { "name": "child-noise-2", "nested": { "prop": "child-prop-3" } }, { "name": "child-signal-3", "nested": { "prop": "child-prop-3" } } ], "noise2": [ { "name": "child-signal-1", "nested": { "prop": "child-prop-3" } }, { "name": "child-noise-2", "nested": { "prop": "child-prop-3" } }, { "name": "child-signal-3", "nested": { "prop": "child-prop-3" } } ] } { "noise1": 5, "signal": [ { "name": "child-signal-1", "nested": { "prop": "child-prop-3" } }, { "name": "child-noise-2", "nested": { "prop": "child-prop-3" } }, { "name": "child-signal-3", "nested": { "prop": "child-prop-3" } } ], "noise2": [ { "name": "child-signal-1", "nested": { "prop": "child-prop-3" } }, { "name": "child-noise-2", "nested": { "prop": "child-prop-3" } }, { "name": "child-signal-3", "nested": { "prop": "child-prop-3" } } ] }
核心疑问
- 为什么
exp as $x | .的输出是重复两次的原始输入,而非单次输出? - 明明管道右侧没有引用
$matches变量,为什么exp的结果数量会直接影响最终输出的次数?
内容的提问来源于stack exchange,提问作者Daniel Kaplan
相关产品推荐
相关产品推荐

