简体   繁体   中英

How to split complex JSON file into multiple files by Python

I am currently splitting Json file.

The structure of JSON file is like this:

{
    "id": 2131424,
    "file": "video_2131424_1938263.mp4",
    "metadata": {
        "width": 3840,
        "height": 2160,
        "duration": 312.83,
        "fps": 30,
        "frames": 9385,
        "created": "Sun Jan 17 17:48:52 2021"
    },
    "frames": [
        {
            "number": 207,
            "image": "frame_207.jpg",
            "annotations": [
                {
                    "label": {
                        "x": 730,
                        "y": 130,
                        "width": 62,
                        "height": 152
                    },
                    "category": {
                        "code": "child",
                        "attributes": [
                            {
                                "code": "global_id",
                                "value": "7148"
                            }
                        ]
                    }
                },
                {
                    "label": {
                        "x": 815,
                        "y": 81,
                        "width": 106,
                        "height": 197
                    },
                    "category": {
                        "code": "person",
                        "attributes": []
                    }
                }
            ]
        },
        {
            "number": 221,
            "image": "frame_221.jpg",
            "annotations": [
                {
                    "label": {
                        "x": 730,
                        "y": 130,
                        "width": 64,
                        "height": 160
                    },
                    "category": {
                        "code": "child",
                        "attributes": [
                            {
                                "code": "global_id",
                                "value": "7148"
                            }
                        ]
                    }
                },
                {
                    "label": {
                        "x": 819,
                        "y": 82,
                        "width": 106,
                        "height": 200
                    },
                    "category": {
                        "code": "person",
                        "attributes": []
                    }
                }
            ]
        },
        {
            "number": 236,
            "image": "frame_236.jpg",
            "annotations": [
                {
                    "label": {
                        "x": 731,
                        "y": 135,
                        "width": 74,
                        "height": 160
                    },
                    "category": {
                        "code": "child",
                        "attributes": [
                            {
                                "code": "global_id",
                                "value": "7148"
                            }
                        ]
                    }
                },
                {
                    "label": {
                        "x": 821,
                        "y": 83,
                        "width": 106,
                        "height": 206
                    },
                    "category": {
                        "code": "person",
                        "attributes": []
                    }
                }
            ]
        },

I have to extract [x, y, width, height] from each label. I tried some code like this:

file = json.load(open('annotation_2131424.json'))
file['frames'][i]['annotations'][j]['label']['x']

But I cannot split JSON. I tried like this but I cannot run...

I hope I've understood your question right. To get x , y , width , height from each label ( dct is your dictionary from the question):

out = [
    [
        [
            a["label"]["x"],
            a["label"]["y"],
            a["label"]["width"],
            a["label"]["height"],
        ]
        for a in frame["annotations"]
    ]
    for frame in dct["frames"]
]

print(out)

Prints:

[
    [[730, 130, 62, 152], [815, 81, 106, 197]],
    [[730, 130, 64, 160], [819, 82, 106, 200]],
    [[731, 135, 74, 160], [821, 83, 106, 206]],
]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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