繁体   English   中英

如何通过在 json 文件中使用 jq 仅替换一个键/值对?

[英]how to replace only one key/value pair by using jq in json file?

我的 json 模板是这样的:

{ 
  "interface_settings": [
    {
      "name": "lan", 
      "status": "$status", 
      ...
    },
    {
     "name": "lte1",
     "status": "$status", 
     ...
    },
    { 
      ...
    }
  ],
  ...
}

还有我的 jq 命令:

jq '.interface_settings[].status="up"' <my_json_template file> 

将更新 interface_settings 部分中的两个状态值。 我怎么可能只是换了一个?

假设我想更新名称为“lan”的状态

更新所有此类对象的一种方法是:

.interface_settings |= map( if .name == "lan" then .status = "up" else . end)

只是第一个这样的

.interface_settings |= (reduce .[] as $x (null;
  if .done
  then .ans += [$x]
  elif $x.name == "lan"
  then .ans += [$x | .status = "up"] | .done = true
  else .ans += [$x]
  end) | .ans)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM