Home > Back-end >  Keep certain Values from JSON obj, deeper in their structure
Keep certain Values from JSON obj, deeper in their structure

Time:11-01

I am using Python in AWS services.

I have this large JSON object:

{'TextDetections': [{'DetectedText': 'AR', 'Type': 'LINE', 'Id': 0, 'Confidence': 100.0, 'Geometry': {'BoundingBox': {'Width': 0.08533523976802826, 'Height': 0.0377860888838768, 'Left': 0.46759268641471863, 'Top': 0.32694804668426514}, 'Polygon': [{'X': 0.46759268641471863, 'Y': 0.32784122228622437}, {'X': 0.5520487427711487, 'Y': 0.32694804668426514}, {'X': 0.5529279112815857, 'Y': 0.3638409674167633}, {'X': 0.468471884727478, 'Y': 0.36473414301872253}]}}, {'DetectedText': '6946435055', 'Type': 'LINE', 'Id': 1, 'Confidence': 100.0, 'Geometry': {'BoundingBox': {'Width': 0.12299089878797531, 'Height': 0.030796218663454056, 'Left': 0.46372532844543457, 'Top': 0.44261065125465393}, 'Polygon': [{'X': 0.46621620655059814, 'Y': 0.44261065125465393}, {'X': 0.5867162346839905, 'Y': 0.44781556725502014}, {'X': 0.5842253565788269, 'Y': 0.4734068810939789}, {'X': 0.46372532844543457, 'Y': 0.4682019352912903}]}}, {'DetectedText': 'AR', 'Type': 'WORD', 'Id': 2, 'ParentId': 0, 'Confidence': 100.0, 'Geometry': {'BoundingBox': {'Width': 0.0833367109298706, 'Height': 0.03677643463015556, 'Left': 0.46846845746040344, 'Top': 0.3278319537639618}, 'Polygon': [{'X': 0.46846845746040344, 'Y': 0.3278319537639618}, {'X': 0.5518018007278442, 'Y': 0.3270817697048187}, {'X': 0.5529279112815857, 'Y': 0.3638409674167633}, {'X': 0.46846845746040344, 'Y': 0.36459115147590637}]}}, {'DetectedText': '6946435055', 'Type': 'WORD', 'Id': 3, 'ParentId': 1, 'Confidence': 100.0, 'Geometry': {'BoundingBox': {'Width': 0.12060987204313278, 'Height': 0.025729140266776085, 'Left': 0.46621620655059814, 'Top': 0.44261065125465393}, 'Polygon': [{'X': 0.46621620655059814, 'Y': 0.44261065125465393}, {'X': 0.5867117047309875, 'Y': 0.4478619694709778}, {'X': 0.5833333134651184, 'Y': 0.4733683466911316}, {'X': 0.46396395564079285, 'Y': 0.46811702847480774}]}}], 'TextModelVersion': '3.0', 'ResponseMetadata': {'RequestId': 'a58ba81f-1b35-437e-a926-64dcdc2d7fbf', 'HTTPStatusCode': 200, 'HTTPHeaders': {'x-amzn-requestid': 'a58ba81f-1b35-437e-a926-64dcdc2d7fbf', 'content-type': 'application/x-amz-json-1.1', 'content-length': '1708', 'date': 'Tue, 26 Oct 2021 19:05:38 GMT'}, 'RetryAttempts': 0}}

I have already isolated the detected text info:

for item in response['TextDetections']:
print(text['DetectedText'])

and I can also go further by assigning the other objects of the main item:

text = item['DetectedText']
type = item['Type']
conf = item['Confidence']

etc.

My question is: how do I isolate the item within the item within, i.e. in deeper levels? To be more specific, I would like to do something like:

left = item["DetectedText":{"Geometry":{"BoundingBox":{"Left"}}}]

this does not seem to work, no matter in how many ways I tried to syntax it.

CodePudding user response:

It should be:

for item in response['TextDetections']:
    left = item['Geometry']['BoundingBox']['Left']
    print(left)
  • Related