admin管理员组文章数量:1336643
I have a template JSON file like this:
[
{
"fields": [
{
"name": "body_t",
"value": "TEST"
}
],
"id": "abc123"
}
]
I need to replace the "value" of the "body_t" (which is set to TEST in the template).
jq '.[].fields[] | select(.name == "body_t") | .value = "'"TEST"'"' template.json
I receive back the following error:
Cannot index string with string "fields"
What am I doing wrong here?
I have a template JSON file like this:
[
{
"fields": [
{
"name": "body_t",
"value": "TEST"
}
],
"id": "abc123"
}
]
I need to replace the "value" of the "body_t" (which is set to TEST in the template).
jq '.[].fields[] | select(.name == "body_t") | .value = "'"TEST"'"' template.json
I receive back the following error:
Cannot index string with string "fields"
What am I doing wrong here?
Share Improve this question edited Nov 19, 2024 at 15:43 Gilles Quénot 186k43 gold badges231 silver badges229 bronze badges asked Nov 19, 2024 at 15:40 Terry Chambers - OnixTerry Chambers - Onix 5973 silver badges10 bronze badges2 Answers
Reset to default 1If you want to keep the original structure, I'd use:
map(.fields[] |= (select(.name == "body_t").value |= "Some value"))
[
{
"fields": [
{
"name": "body_t",
"value": "Some value"
}
],
"id": "abc123"
}
]
JqPlay Demo
What I would do:
$ jq '.[].fields[] | select(.name == "body_t") | .value = "TESTOS"' file
{
"name": "body_t",
"value": "TESTOS"
}
本文标签: Using jq to replace the value of a JSON field is not working correctlyStack Overflow
版权声明:本文标题:Using jq to replace the value of a JSON field is not working correctly - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742417185a2470942.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论