Bash JQ. How can I change a pair of key values ​​from a json file containing a list of objects?

I am using jq to work with a large json file. It looks something like this:

FILE1.json

{
  "person": [
      {
          "name": "sam",
          "age": "40",
          "weight": "180",
          "height": "6"
       },
       {
          "name": "peter",
          "age": "41",
          "weight": "180",
          "height": "6.1"
       },
       {
          "name": "mike",
          "age": "40",
          "weight": "200",
          "height": "5.9"
       },
       {
          "name": "ethan",
          "age": "41",
          "weight": "190",
          "height": "6"
       }
  ]
}

      

I want to use jq tool to change the weight value from 200 to 195 where the name is "mike".
How can i do this?

+3


source to share


1 answer


The idea is to update the person array, where the object having the name "mike"

will be changed to have weight

"195"

. Otherwise, he just missed.

.person |= map(
    if .name == "mike"
        then .weight = "195"
        else .
    end)

      



Or more succinctly, find people to update and update them:

(.person[] | select(.name == "mike")).weight = "195"

      

+7


source







All Articles