[英]how to replace only one key/value pair by using jq in json file?
My json template is as of this:我的 json 模板是这样的:
{
"interface_settings": [
{
"name": "lan",
"status": "$status",
...
},
{
"name": "lte1",
"status": "$status",
...
},
{
...
}
],
...
}
And my jq command:还有我的 jq 命令:
jq '.interface_settings[].status="up"' <my_json_template file>
will update both status values within the interface_settings section.将更新 interface_settings 部分中的两个状态值。 How may I just have one changed ?我怎么可能只是换了一个?
let's say I want to update the status where the name is "lan"假设我想更新名称为“lan”的状态
One way to update all such objects would be:更新所有此类对象的一种方法是:
.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.