如何将数字按顺序依次填入json value 中?

有这样一个json,其中3个label全部为“t”,我需要将label依次替换成“1”,“2”,“3” 请问我如何将数字依次更改到label中?

↓原json:

{
  "version": "4.5.10",
  "flags": {},
  "shapes": [
    {
      "label": "t",
      "points": [
        [
          2132.5,
          648.0
        ]
      ],
      "group_id": null,
      "shape_type": "point",
      "flags": {}
    },
    {
      "label": "t",
      "points": [
        [
          2135.0,
          748.0
        ]
      ],
      "group_id": null,
      "shape_type": "point",
      "flags": {}
    },
    {
      "label": "t",
      "points": [
        [
          2130.0,
          850.5
        ]
      ],
      "group_id": null,
      "shape_type": "point",
      "flags": {}
    }
  ],
  "imagePath": "1653041701.jpg",
  "imageData": null,
  "imageHeight": 2160,
  "imageWidth": 3840
}

↓希望替换成的json:

{
  "version": "4.5.10",
  "flags": {},
  "shapes": [
    {
      "label": "1",
      "points": [
        [
          2132.5,
          648.0
        ]
      ],
      "group_id": null,
      "shape_type": "point",
      "flags": {}
    },
    {
      "label": "2",
      "points": [
        [
          2135.0,
          748.0
        ]
      ],
      "group_id": null,
      "shape_type": "point",
      "flags": {}
    },
    {
      "label": "3",
      "points": [
        [
          2130.0,
          850.5
        ]
      ],
      "group_id": null,
      "shape_type": "point",
      "flags": {}
    }
  ],
  "imagePath": "1653041701.jpg",
  "imageData": null,
  "imageHeight": 2160,
  "imageWidth": 3840
}


json_data = {
  "version": "4.5.10",
  "flags": {},
  "shapes": [
    {
      "label": "t",
      "points": [
        [
          2132.5,
          648.0
        ]
      ],
      "group_id": None,
      "shape_type": "point",
      "flags": {}
    },
    {
      "label": "t",
      "points": [
        [
          2135.0,
          748.0
        ]
      ],
      "group_id": None,
      "shape_type": "point",
      "flags": {}
    },
    {
      "label": "t",
      "points": [
        [
          2130.0,
          850.5
        ]
      ],
      "group_id": None,
      "shape_type": "point",
      "flags": {}
    }
  ],
  "imagePath": "1653041701.jpg",
  "imageData": None,
  "imageHeight": 2160,
  "imageWidth": 3840
}

for index, temp in enumerate(json_data['shapes']):
    temp['label'] = index + 1
print(json_data)

用json库操作,用json.loads()函数加载原始字符串,然后按字典操作,参考代码如下

import json


def replace_json_label(origin_str: str) -> str:
    origin_json = json.loads(origin_str)
    shapes = origin_json['shapes']

    idx = 1
    for item in shapes:
        item['label'] = idx
        idx += 1
    return json.dumps(origin_json)


if __name__ == '__main__':
    origin = """
    {
        "version": "4.5.10",
        "flags": {},
        "shapes": [
            {
                "label": "t",
                "points": [
                    [
                        2132.5,
                        648.0
                    ]
                ],
                "group_id": null,
                "shape_type": "point",
                "flags": {}
            },
            {
                "label": "t",
                "points": [
                    [
                        2135.0,
                        748.0
                    ]
                ],
                "group_id": null,
                "shape_type": "point",
                "flags": {}
            },
            {
                "label": "t",
                "points": [
                    [
                        2130.0,
                        850.5
                    ]
                ],
                "group_id": null,
                "shape_type": "point",
                "flags": {}
            }
        ],
        "imagePath": "1653041701.jpg",
        "imageData": null,
        "imageHeight": 2160,
        "imageWidth": 3840
    }
    """

    res = replace_json_label(origin)
    print(res)


循环,然后找个唯一ID判断