【发布时间】:2020-02-18 04:54:09
【问题描述】:
为什么“Append new keypair to JSON file with jq”中接受的答案在我的情况下不起作用?
我有一个文件newFile.json,其中包含:
[ { "groupId": 11, "lowerThreshold": 33, "target": { "alarm_id": 22 },
"thresholdPeriod": 3, "upperThreshold": 44 }, { "groupId": 31,
"lowerThreshold": 33, "target": { "alarm_id": 122 },
"thresholdPeriod": 3, "upperThreshold": 44 } ]
我想在 ruleGroup.json 中的数组中插入另一个值:
{ "groupId": "0", "target": { "alarm_id": "69" }, "upperThreshold":
"20", "lowerThreshold": "10", "thresholdPeriod": "5" }
根据接受的答案,我将ruleGroup.json 保存到obj。
当我跑步时
jq --argjson obj '$obj' '. + [$obj]' < newFile.json
它返回:
[ "{\n \"groupId\": \"0\",\n \"target\": {\n \"alarm_id\": \"69\"\n
},\n \"upperThreshold\": \"20\",\n \"lowerThreshold\": \"10\",\n
\"thresholdPeriod\": \"5\"\n}" ]
这类似于$obj 值本身。它没有创建将这个值附加到数组的预期结果:
[ { "groupId": 11, "lowerThreshold": 33, "target": { "alarm_id": 22 },
"thresholdPeriod": 3, "upperThreshold": 44 }, { "groupId": 31,
"lowerThreshold": 33, "target": { "alarm_id": 122 },
"thresholdPeriod": 3, "upperThreshold": 44 }, { "groupId": "0",
"target": { "alarm_id": "69" }, "upperThreshold": "20",
"lowerThreshold": "10", "thresholdPeriod": "5" } ]
【问题讨论】:
-
你是怎么尝试的?对我来说效果很好
-
请阅读Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers? - 总结是这不是解决志愿者的理想方式,并且可能会适得其反。请不要将此添加到您的问题中。
-
请阅读“How do I format my posts...”和“How do I format my code blocks?”。未能正确格式化会减慢答案的速度,并且通常会完全阻止它们。既然您要求我们为您提供帮助,我们希望您努力提出问题。请查看“How to Ask”、“Stack Overflow question checklist”及其所有链接页面。