[
{
"frame_id":2,
"filename":"sample_imgs/2.jpg",
"objects": [
{"class_id":8, "name":"boat", "relative_coordinates":{"left_x":1547, "top_y":1126, "width": 298, "height": 149}, "confidence":0.348690},
{"class_id":0, "name":"person", "relative_coordinates":{"left_x": 128, "top_y":1327, "width": 30, "height": 89}, "confidence":0.972903},
{"class_id":0, "name":"person", "relative_coordinates":{"left_x": 179, "top_y":1332, "width": 26, "height": 72}, "confidence":0.689888},
{"class_id":0, "name":"person", "relative_coordinates":{"left_x": 79, "top_y":1324, "width": 29, "height": 73}, "confidence":0.663020},
{"class_id":0, "name":"person", "relative_coordinates":{"left_x": 520, "top_y":1374, "width": 40, "height": 79}, "confidence":0.657240},
{"class_id":0, "name":"person", "relative_coordinates":{"left_x": 565, "top_y":1367, "width": 36, "height": 84}, "confidence":0.447690},
{"class_id":0, "name":"person", "relative_coordinates":{"left_x": 521, "top_y":1355, "width": 34, "height": 41}, "confidence":0.412204},
{"class_id":0, "name":"person", "relative_coordinates":{"left_x": 159, "top_y":1318, "width": 27, "height": 77}, "confidence":0.306808}
]
How can I parse the file and count the number of IDs present in the JSON file using Python? The code i used displayed all the data, but id like something of narrowing down eg
"person": 7,
"boat": 1
Here's my code
import json
from collections import Counter
with open('/home/output/result.json') as json_data:
data = json.load(json_data)
c = Counter(k[4:] for d in data for k, v in d.items() if k.startswith('class_id'))
print(data)
print(json.dumps(c, indent=2, sort_keys=True))
nameelement, why are you usingk.startswith('class_id')?