Using jq to replace the value of a JSON field is not working correctly - Stack Overflow

I have a template JSON file like this:[{"fields": [{"name": "body_t",&qu

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 badges
Add a comment  | 

2 Answers 2

Reset to default 1

If 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"
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742417185a4439968.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信