0

I'm using jq to modify a json file but I'm having issues with nested objects. I need to find an object inside of an array of objects and then modify a nested object inside of that object. I'm able to find the correct object and modify a key/value pair inside of that object, but modifying a key/value pair inside of a nested object is giving me issues. I’m sure there is a way to accomplish this with jq but I cannot find it. For this example I'm trying to modify the "pos_color" value for the track where the label is AX11S1.bw.

# find correct object and change key/value command ( this will change the min_score from 0 to 200 )
jq '.tracks |= map(if .label=="AX11S1.bw" then . + {"min_score":"200"} else . end)' trackList.json 

# Example JSON
{
  "tracks": [
    {
      "style": {
        "clip_marker_color": "red",
        "neg_color": "#005EFF",
        "pos_color": "blue",
        "height": 100
      },
      "variance_band": true,
      "max_score": 100,
      "label": "AX11S1.bw",
      "min_score": 0
    },
    {
      "style": {
        "clip_marker_color": "red",
        "neg_color": "#005EFF",
        "pos_color": "blue",
        "height": 100
      },
      "variance_band": true,
      "max_score": 100,
      "label": "AX11S2.bw",
      "min_score": 0
    },
    {
      "style": {
        "clip_marker_color": "red",
        "neg_color": "#005EFF",
        "pos_color": "blue",
        "height": 100
      },
      "variance_band": true,
      "max_score": 100,
      "label": "AX11S3.bw",
      "min_score": 0
    }
  ]
}
0

1 Answer 1

1
.tracks |= map(if .label=="AX11S1.bw" 
               then .style.pos_color = "NEW VALUE" 
               else . end)
Sign up to request clarification or add additional context in comments.

1 Comment

Exactly what I was looking for, Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.