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
}
]
}