【发布时间】:2021-06-21 23:57:36
【问题描述】:
我知道这必须很简单,但由于某种原因,它使我无法找到给定条件的元素并修改其中一个字段。文档应在编辑后完全输出(sed 样式)。
{
"state": "wait",
"steps": {
"step1": [
{ "name":"Foo", "state":"wait" },
{ "name":"Bar", "state":"wait" }
],
"step2": [
{ "name":"Foo", "state":"wait" },
{ "name":"Zoinks", "state":"ready" }
],
"step3": [
{ "name":"Foo", "state":"cancel" }
]
}
}
我希望这样的事情应该可行。
jq '. | (select(.steps[][].name=="Foo" and .steps[][].state=="wait") |= . + {.state:"Ready"}'
或
jq '. | (select(.steps[][]) | if (.name=="Foo" and .state=="wait") then (.state="Ready") else . end)
当然,期望的输出是
{
"state": "wait",
"steps": {
"step1": [
{ "name":"Foo", "state":"ready" },
{ "name":"Bar", "state":"wait" }
],
"step2": [
{ "name":"Foo", "state":"ready" },
{ "name":"Zoinks", "state":"ready" }
],
"step3": [
{ "name":"Foo", "state":"cancel" }
]
}
}
相反,当我没有遇到神秘错误时,我要么修改文档中的顶级字段,要么修改 所有 元素的字段,或者多次重复整个文档。
非常感谢任何见解。
谢谢。
附言是否有比 [] 更好的语法来通配步骤下的命名元素?还是通过管道来识别select发现的索引?
【问题讨论】: