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

如何使用yq删除所有标量值为false的键?

使用yq删除所有值为false的标量键

要删除YAML中所有值为false的标量键,可以使用以下yq命令:

yq 'walk(if type == "map" then with_entries(select(.value != false)) else . end)' input.yaml

命令解释

  • walk(...):递归遍历YAML结构中的每一个节点
  • if type == "map" then ... else . end:仅对映射(键值对集合)类型的节点进行处理,非映射节点保持原样
  • with_entries(select(.value != false)):过滤映射中的键值对,只保留值不等于false的条目,从而删除所有值为false的键

示例

输入YAML

project: false_deletion
fields:
  name:
    numeric: false
    value: John
  age:
    value: 25
    numeric: true
  address:
    value: 123 Main St
    numeric: false

执行命令后输出

project: false_deletion
fields:
  name:
    value: John
  age:
    value: 25
    numeric: true
  address:
    value: 123 Main St

内容的提问来源于stack exchange,提问作者Mark Miller

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.19 00:10:31