选择条件后将记录添加到 JSON 对象
我有以下 json:
{
"thermostats": [
{
"name": "Thermostat1",
"identifier": 111111111111
},
{
"name": "Thermostat2",
"identifier": 222222222222
}
]
}
我想将以下 {default: yes}
添加到我选择的 json 中。
{
"thermostats": [
{
"name": "Thermostat1",
"identifier": 111111111111,
"default": "yes"
},
{
"name": "Thermostat2",
"identifier": 222222222222
}
]
}
我有以下内容,但尚未弄清楚如何添加记录
q '.thermostats[] | select (.identifier == "1111111111")
我已经尝试过。 += {默认:是}
| 。 += {默认:是}
|= 。 +
但我偏离了错误的轨道。
谢谢
I have the following json:
{
"thermostats": [
{
"name": "Thermostat1",
"identifier": 111111111111
},
{
"name": "Thermostat2",
"identifier": 222222222222
}
]
}
I'd like to add the following {default: yes}
to the one that I select.
{
"thermostats": [
{
"name": "Thermostat1",
"identifier": 111111111111,
"default": "yes"
},
{
"name": "Thermostat2",
"identifier": 222222222222
}
]
}
I have the following but haven't figured out how to add the record
q '.thermostats[] | select (.identifier == "1111111111")
I've tried . += {default: yes}
| . += {default: yes}
|= . +
but I'm going off the wrong track.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将引号标记
"
放在您要搜索的数字类型上。引号仅应在选择/匹配字符串类型时应用。使用
+=
将对象添加到所选内容上入口jqplay 演示 - https://jqplay.org/s/k5tForQ9HT
如果您要通过比赛标识符作为参数,请参阅 使用 jq --arg 传递的数字参数与 == 不匹配数据的答案,因为
--arg
默认情况下将您的参数定义为字符串类型,需要在您的select
中将其修改为数字类型以进行比较。 tonumber 或通过它作为 argjsonDrop the quote marker
"
on the numeric type you are trying to search. Quotes should apply only when selecting/matching string types.Use
+=
to add the object on the selected entryjqplay demo - https://jqplay.org/s/k5tForQ9HT
If you were to pass the match identifier as an argument, do refer to the answers at Numeric argument passed with jq --arg not matching data with ==, because
--arg
by default defines your argument to be string type, which needs to be modified inside yourselect
to numeric type for comparison. So use$thermoid|tonumber
or pass it asargjson