用jq修改嵌套对象

时间:2019-11-09 21:48:35

标签: json object jq

我正在使用jq修改json文件,但是嵌套对象存在问题。我需要在对象数组内找到一个对象,然后在该对象内修改嵌套对象。我能够找到正确的对象并修改该对象内部的键/值对,但是修改嵌套对象内部的键/值对给我带来了问题。我敢肯定有一种方法可以用jq完成,但是我找不到。对于此示例,我尝试修改标签为AX11S1.bw的轨道的“ pos_color”值。

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

1 个答案:

答案 0 :(得分:1)

.tracks |= map(if .label=="AX11S1.bw" 
               then .style.pos_color = "NEW VALUE" 
               else . end)
相关问题