Extract only certain values from json files

Asked 2 years ago, Updated 2 years ago, 37 views

{
    "1.png896616":{
        "filename":"1.png",
        "size":896616,
        "regions":[
            {
                "shape_attributes":{
                    "name":"polygon",
                    "all_points_x":[
                        286,284,......
                    ],
                    "all_points_y":[
                        94,93,....
                    ]
                },
                "region_attributes":{
                     "sweet_pepper":"YELLOW"
                }
            },
            {
                "shape_attributes":{
                    "name":"polygon",
                    "all_points_x":[
                        455,457,...
                    ],
                    "all_points_y":[
                        194,202,...
                    ]
                },
                "region_attributes":{
                    "sweet_pepper":"YELLOW"
                }
            },
            {
                "shape_attributes":{
                    "name":"polygon",
                    "all_points_x":[
                        295,294,...
                    ],
                    "all_points_y":[
                        426,437,...
                    ]
                },
                "region_attributes":{
                    "sweet_pepper":"RED"
                }
            }
        ],
        "file_attributes":{}
    },
    "2.png810690":{
        "filename":"2.png",
        "size":810690,
        "regions":[
            {
                "shape_attributes":{
                    "name":"polygon",
                    "all_points_x":[
                        257,246,...
                    ],
                    "all_points_y":[
                        18,16,...
                        ]
                    },
                    "region_attributes":{
                        "sweet_pepper":"YELLOW"
                    }
                },
                {
                    "shape_attributes":{
                        "name":"polygon",
                        "all_points_x":[
                            439,466,...
                        ],
                        "all_points_y":[
                            93,115,...
                        ]
                    },
                    "region_attributes":{
                        "sweet_pepper":"YELLOW"
                    }
                },
                {
                    "shape_attributes":{
                        "name":"polygon",
                        "all_points_x":[
                            283,284...
                        ],
                        "all_points_y":[
                            355,381,...
                        ]
                    },
                    "region_attributes":{
                        "sweet_pepper":"RED"
                    }
                },
                {
                    "shape_attributes":{
                        "name":"polygon",
                        "all_points_x":[
                            193,176,...
                        ],
                        "all_points_y":[
                            169,174,...
                        ]
                    },
                    "region_attributes":{
                        "sweet_pepper":"GREEN"
                    }
                }
            ],
            "file_attributes":{
            }
    }
}

This is the format of the json file. Here we only put json files for two images, but actually there are more than 40.

I only want to extract values for all_points_x, all_points_y, but I don't know how. The values for all_points_x, all_points_y are included in 1.png896616 (or 2.png810690) -> regions -> shape_attributes.

If there is one image, we extracted all_points_x and all_points_y, but since there are more than two of them, I don't know what to do.

Do I have to extract each image separately?

python json

2022-09-20 16:20

1 Answers

import json
from os import readlink

with open('json path', 'r') as f:
    json_data = json.load(f)

keyList = json_data.keys()
key_list = list(keyList)
#print(key_list)

for e in range(2):
    key = key_list[e]
    print(key)
    for i, j in enumerate(json_data[key]['regions']):
        print("all_poins_x : ", i,j['shape_attributes']['all_points_x'])
        print("all_poins_y : ", i,j['shape_attributes']['all_points_y'])

That's what I chose. If there is anyone who wants to refer to it, please see!


2022-09-20 16:20

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.