You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何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"
      }
    }
  ]
}

核心疑问

  1. 为什么exp as $x | .的输出是重复两次的原始输入,而非单次输出?
  2. 明明管道右侧没有引用$matches变量,为什么exp的结果数量会直接影响最终输出的次数?

内容的提问来源于stack exchange,提问作者Daniel Kaplan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 01:44:54