'Reading values from a key within a Directory within a List from a JSON file using Python
I've got a rather ML ugly JSON file that I'm trying to pull values from.
Below is the first entry on the list: [ {"ID":"ckzjqmtvf3ltd0z7yhogt3ttm", "DataRow ID":"ckzjq9ry03hfp0zux8xwkhkvv", "Label":{"objects": [{"featureId":"ckzjqnrn600012469dfbkfr1q", "schemaId":"ckzjqkttm3m8v0z78e7rdbkda", "color":"#1CE6FF", "title":"Weed", "value":"weed", "bbox":{"top":1003, "left":810, "height":848, "width":881}, {"featureId":"ckzjqo6iy000724690onwpxax", "schemaId":"ckzjqkttm3m8v0z78e7rdbkda", "color":"#1CE6FF", "title":"Weed", "value":"weed", "bbox"{"top":1780, "left":410, "height":264, "width":254}, {"featureId":"ckzjqobrv000a24697tc84nrr", "schemaId":"ckzjqkttm3m8v0z78e7rdbkda", "color":"#1CE6FF", "title":"Weed", "value":"weed", "bbox":{"top":1060, "left":1799, "height":471, "width":523},]
I am trying to extract the title and bbox information.
I have tried data["Label"][0]["title"], but I get:
KeyError: 0
Any help would be amazing!
Solution 1:[1]
Code to read title and bbox information of all items in json file:
import json
with open("data.json") as file:
data = json.load(file)
for item in data:
objects = item['Label']['objects']
for obj in objects:
title = obj['title']
bbox = obj['bbox']
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | gajendragarg |
