0

I need to get a particular key:value from a json file.

Code used to open json file :

import json
data = repr(open('file.json', 'rb').read())
print(data)

Output( Json file) :

b'b\'{"image":{"width":750,"height":1127,"orientation":1},"objects":[{"type":"face","boundingBox":{"x":132,"y":180,"height":619,"width":513},"landmarks":{"faceContour":[[162,375],[162,440],[165,504],[175,568],[193,628],[226,686],[265,740],[313,784],[373,799],[436,793],[494,758],[543,712],[586,660],[616,601],[631,534],[639,468],[645,400]],"noseBridge":[[387,396],[385,441],[381,485],[378,532]],"noseBall":[[322,547],[350,560],[380,572],[412,563],[442,554]],"eyebrowRight":[[419,348],[466,329],[516,325],[564,339],[595,377]],"eyebrowLeft":[[197,358],[224,325],[269,315],[316,325],[358,347]],"eyeRight":[[448,419],[479,404],[510,405],[539,416],[511,425],[480,424]],"eyeRightCenter":[[495,416]],"eyeLeft":[[242,402],[269,393],[299,396],[329,412],[297,414],[267,413]],"eyeLeftCenter":[[284,405]],"mouthOuter":[[269,596],[310,598],[349,598],[379,606],[411,601],[456,606],[502,609],[456,663],[412,684],[378,687],[345,680],[307,652]],"mouthInner":[[284,603],[349,610],[379,615],[411,613],[487,614],[412,652],[379,656],[348,648]]},"attributes":{"gender":"male","genderConfidence":0.8385,"age":24,"ageConfidence":0.8419,"emotion":"happiness","emotionConfidence":1.0,"emotionsAll":{"neutral":0.0,"sadness":0.0,"disgust":0.0,"anger":0.0,"surprise":0.0,"fear":0.0,"happiness":1.0},"pose":{"pitch":-5.7105,"roll":0.2941,"yaw":1.8646},"race":{"asian":0.0002,"black":0.0001,"white":0.9989},"eyewear":{"sunglasses":0.0,"glasses":0.0},"hair":{"color":{"blond":0.0,"black":0.9994,"brown":0.0},"cut":{"short":0.9999,"long":0.0,"bald":0.0},"facial":{"beard":0.0,"mustache":0.0,"stubble":0.2543}},"frontal":true}},{"type":"person","boundingBox":{"x":20,"y":60,"height":1053,"width":696}}],"requestId":"1239be0c782440bd828cee21dbc2baa1"}\'\r\n'

From this Json file, I need to extract a particular key:value. For example, I need to get the emotionsAll (Key) from the file.

I tried using :

print(data['emotionsAll'][0]['neutral']) 

error i got :

Traceback (most recent call last):   File "C:\Users\HP\Downloads\Final Project\getfile.py", line 6, in <module>
    print(data['emotionsAll'][0]['neutral']) TypeError: string indices must be integers

So, Is there any other way to get a particular key:value ?

  • try `data['emotionsAll']['neutral']`. `emotionsAll` is not a list so you can't access it via integer indices. – Jarek.D Feb 21 '19 at 10:57
  • Nope ! That was about how to open a json file . My question is how would i read them as like a dictionary :) @Fabian –  Feb 21 '19 at 10:57
  • Still i get : Traceback (most recent call last): File "C:\Users\HP\Downloads\Final Project\getfile.py", line 5, in data['emotionsAll']['neutral'] TypeError: string indices must be integers –  Feb 21 '19 at 10:59
  • @Sai if you check you just read it in wrong ... if you open and load json format correctly you are able to access like dict... – Fabian Feb 21 '19 at 12:17
  • tried opening :raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) –  Feb 21 '19 at 12:32

2 Answers2

1

Try using the json package

Contents of file.json

{"image":{"width":750,"height":1127,"orientation":1},"objects":[{"type":"face","boundingBox":{"x":132,"y":180,"height":619,"width":513},"landmarks":{"faceContour":[[162,375],[162,440],[165,504],[175,568],[193,628],[226,686],[265,740],[313,784],[373,799],[436,793],[494,758],[543,712],[586,660],[616,601],[631,534],[639,468],[645,400]],"noseBridge":[[387,396],[385,441],[381,485],[378,532]],"noseBall":[[322,547],[350,560],[380,572],[412,563],[442,554]],"eyebrowRight":[[419,348],[466,329],[516,325],[564,339],[595,377]],"eyebrowLeft":[[197,358],[224,325],[269,315],[316,325],[358,347]],"eyeRight":[[448,419],[479,404],[510,405],[539,416],[511,425],[480,424]],"eyeRightCenter":[[495,416]],"eyeLeft":[[242,402],[269,393],[299,396],[329,412],[297,414],[267,413]],"eyeLeftCenter":[[284,405]],"mouthOuter":[[269,596],[310,598],[349,598],[379,606],[411,601],[456,606],[502,609],[456,663],[412,684],[378,687],[345,680],[307,652]],"mouthInner":[[284,603],[349,610],[379,615],[411,613],[487,614],[412,652],[379,656],[348,648]]},"attributes":{"gender":"male","genderConfidence":0.8385,"age":24,"ageConfidence":0.8419,"emotion":"happiness","emotionConfidence":1.0,"emotionsAll":{"neutral":0.0,"sadness":0.0,"disgust":0.0,"anger":0.0,"surprise":0.0,"fear":0.0,"happiness":1.0},"pose":{"pitch":-5.7105,"roll":0.2941,"yaw":1.8646},"race":{"asian":0.0002,"black":0.0001,"white":0.9989},"eyewear":{"sunglasses":0.0,"glasses":0.0},"hair":{"color":{"blond":0.0,"black":0.9994,"brown":0.0},"cut":{"short":0.9999,"long":0.0,"bald":0.0},"facial":{"beard":0.0,"mustache":0.0,"stubble":0.2543}},"frontal":true}},{"type":"person","boundingBox":{"x":20,"y":60,"height":1053,"width":696}}],"requestId":"1239be0c782440bd828cee21dbc2baa1"}

Code:

import json

with open('file.json', 'rb') as fp:
    json_data = json.load(fp)

print(json_data['objects'][0]['attributes']['emotionsAll']['neutral'])
bumblebee
  • 1,811
  • 12
  • 19
0

A couple things:

1 - the emotionsAll key is within the objects key, first element in the list [0], attributes key

2 - Your json file was written with the bytes prefix, so when it's being read, it starts your string with b'. You can either a) have that file written without that mode by decoding/encoding, or just manipulate that string.

import json

data = repr(open('file.json', 'rb').read())

data = data.split('{', 1)[-1]
data = data.rsplit('}', 1)[0]

data = ''.join(['{', data, '}'])
jsonObj = json.loads(data)

print(jsonObj['objects'][0]['attributes']['emotionsAll']['neutral']) 

Output:

print(jsonObj['objects'][0]['attributes']['emotionsAll']['neutral']) 
0.0
chitown88
  • 27,527
  • 4
  • 30
  • 59
  • It Works only if the json file content is defined in the json_str . But when i used the same from a external json file (i.e data = repr(open('file.json', 'rb').read()) ) , I doesn't give the same ! –  Feb 21 '19 at 11:04
  • @Sai, true, but I was addressing the other issue of `TypeError: string indices must be integers`. But maybe it's the binary mode that is causing the issue? Look at the edit in the solution and see if that works – chitown88 Feb 21 '19 at 11:18
  • data = repr(open('file.json', 'r').read()) works but once i try print(data['objects'][0]['attributes']['emotionsAll']['neutral']) it doesnt return the value . Instead it gives a error : TypeError: string indices must be integers...... with open('file.json') as fp: data = json.load(fp) when i tried this .. i got :::: Still the same : raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) –  Feb 21 '19 at 11:22
  • I have Send the json file to your email . Please do check it :) –  Feb 21 '19 at 11:35
  • Thanks a lot ! Work fine now :) –  Feb 21 '19 at 12:44
  • I still think a better way would be to make sure that API json file is written without that bytes prefix, but in the mean time, this is a work around – chitown88 Feb 21 '19 at 12:47