重新格式化json文件,使其不包含嵌套数据

2024-04-24 03:12:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在尝试重新格式化json文件,使所有嵌套数据都显示为单个属性。所以本质上,从json示例A中,我将得到不包含任何嵌套数据的json B。我该怎么做呢

JSON A:

       [
    {
     "frame_id":1, 
     "filename":"/content/drive/MyDrive/Clocks/clock.jpg", 
     "objects": [ 
      {"class_id":74, "name":"clock", "relative_coordinates":{"center_x":0.497010, "center_y":0.561621, "width":0.300727, "height":0.557968}, "confidence":0.754266}
     ] 
    }, 
    {
     "frame_id":2, 
     "filename":"/content/drive/MyDrive/Clocks/clock2.jpg", 
     "objects": [ 
      {"class_id":74, "name":"clock", "relative_coordinates":{"center_x":0.651665, "center_y":0.511030, "width":0.673170, "height":1.007840}, "confidence":0.935582}
     ] 
    }
]

JSON B:

    [
{
 "frame_id":1, 
 "filename":"/content/drive/MyDrive/Clocks/clock.jpg",  
 "class_id":74, 
 "name":"clock", 
 "center_x":0.497010, 
 "center_y":0.561621, 
 "width":0.300727, 
 "height":0.557968, 
 "confidence":0.754266
}, 

{
 "frame_id":2, 
 "filename":"/content/drive/MyDrive/Clocks/clock2.jpg", 
 "class_id":74, 
 "name":"clock", 
 "center_x":0.651665, 
 "center_y":0.511030, 
 "width":0.673170, 
 "height":1.007840, 
 "confidence":0.935582
 
}
]

2条回答

递归函数也可用于:

from pprint import pprint

data = [
    {
        "frame_id": 1,
        "filename": "/content/drive/MyDrive/Clocks/clock.jpg",
        "objects": [
            {"class_id": 74, "name": "clock",
             "relative_coordinates": {"center_x": 0.497010, "center_y": 0.561621, "width": 0.300727,
                                      "height": 0.557968}, "confidence": 0.754266}
        ]
    },
    {
        "frame_id": 2,
        "filename": "/content/drive/MyDrive/Clocks/clock2.jpg",
        "objects": [
            {"class_id": 74, "name": "clock",
             "relative_coordinates": {"center_x": 0.651665, "center_y": 0.511030, "width": 0.673170,
                                      "height": 1.007840}, "confidence": 0.935582}
        ]
    }
]


def flatten(x):

    if isinstance(x, dict):
        new_dict = {}
        for k, v in x.items():
            if isinstance(v, dict):
                new_dict.update(v)
            elif isinstance(v, list) and v and isinstance(v[0], dict):
                for e in v:
                    new_dict.update(flatten(e))
            else:
                new_dict[k] = v

        return new_dict

    return x


result = [flatten(d) for d in data]

pprint(result)

输出:

[{'center_x': 0.49701,
  'center_y': 0.561621,
  'class_id': 74,
  'confidence': 0.754266,
  'filename': '/content/drive/MyDrive/Clocks/clock.jpg',
  'frame_id': 1,
  'height': 0.557968,
  'name': 'clock',
  'width': 0.300727},
 {'center_x': 0.651665,
  'center_y': 0.51103,
  'class_id': 74,
  'confidence': 0.935582,
  'filename': '/content/drive/MyDrive/Clocks/clock2.jpg',
  'frame_id': 2,
  'height': 1.00784,
  'name': 'clock',
  'width': 0.67317}]

或者,如果您使用的是Python 3.9+,则可以替换所有出现的:

new_dict.update(v)

使用^{} union运算符:

new_dict |= v

假设objects中只有一个元素,您可以使用dict.popdict.update

data = [{'frame_id': 1, 'filename': '/content/drive/MyDrive/Clocks/clock.jpg', 'objects': [{'class_id': 74, 'name': 'clock', 'relative_coordinates': {'center_x': 0.49701, 'center_y': 0.561621, 'width': 0.300727, 'height': 0.557968}, 'confidence': 0.754266}]}, 
        {'frame_id': 2, 'filename': '/content/drive/MyDrive/Clocks/clock2.jpg', 'objects': [{'class_id': 74, 'name': 'clock', 'relative_coordinates': {'center_x': 0.651665, 'center_y': 0.51103, 'width': 0.67317, 'height': 1.00784}, 'confidence': 0.935582}]}]

result = data.copy()
for d in result:
  obj = d.pop('objects')[0]
  obj.update(obj.pop('relative_coordinates'))
  d.update(obj)

{'center_x': 0.49701,
  'center_y': 0.561621,
  'class_id': 74,
  'confidence': 0.754266,
  'filename': '/content/drive/MyDrive/Clocks/clock.jpg',
  'frame_id': 1,
  'height': 0.557968,
  'name': 'clock',
  'width': 0.300727},
 {'center_x': 0.651665,
  'center_y': 0.51103,
  'class_id': 74,
  'confidence': 0.935582,
  'filename': '/content/drive/MyDrive/Clocks/clock2.jpg',
  'frame_id': 2,
  'height': 1.00784,
  'name': 'clock',
  'width': 0.67317}]

相关问题 更多 >