简体   繁体   English

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

[英]Reformat json file so it does not contain nested data

I have been trying to reformat a json file in a way that all the nested data appears as single attributes.我一直在尝试以所有嵌套数据显示为单个属性的方式重新格式化 json 文件。 So essentially, from json example A, I would get json B that does not contain any of the nested data.所以基本上,从 json 示例 A 中,我会得到不包含任何嵌套数据的 json B。 How should I go about that?我该怎么办?

JSON A: 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: 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
 
}
]

Assuming there will only ever be one element in objects you can use dict.pop and dict.update假设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}]

A recursive function could also be used for this:递归函数也可以用于此:

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)

Output:输出:

[{'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}]

Alternatively, if you're on Python 3.9+ you can replace all occurrences of:或者,如果您使用的是 Python 3.9+,则可以替换所有出现的:

new_dict.update(v)

with the dict union operator:使用dict联合运算符:

new_dict |= v

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM