我有给定的 JSON 并且想要更改所有元素的 id
值,它以 name
元素中的 test
开头:
{
"other-value": "some-id",
"values": [
{
"name": "test-2017-12-01",
"id": "1"
},
{
"name": "othert",
"id": "2"
}
]
}
以下 jq 命令有效 jqplay
jq (.values[] | select(.name == "test-afs").id) |= "NEWID"
但是当我用 startswith
尝试它时它停止工作,我错过了什么? jqplay
(.values[] | select(.name | startswith("test")).id) |= "NEWID"
jq: error (at :14): Invalid path expression near attempt to access element "id" of {"name":"test-afs","id":"id"} exit status 5
最佳答案
你也可以使用map
,像这样:
jq '(.values)|=(map((if .name|startswith("test") then .id="NEWID" else . end)))' file
输出:
{
"other-value": "some-id",
"values": [
{
"name": "test-2017-12-01",
"id": "NEWID"
},
{
"name": "othert",
"id": "2"
}
]
}
关于json - 如果对应于给定键的值以指定字符串开头,则使用 jq 更新 JSON 文档中的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44410394/