如何在json文件中使用jq只替换一个键值对



我的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)

最新更新