skip to Main Content

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?

2

Answers


  1. What I would do:

    $ jq '.[].fields[] | select(.name == "body_t") | .value = "TESTOS"' file
    {
      "name": "body_t",
      "value": "TESTOS"
    }
    
    Login or Signup to reply.
  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search