I am trying to read a json file from S3 bucket.
def getJSONObject(object):
content = object.get()['Body'].read()[1:-1]
content = '{' + content + '}'
data = json.loads(content)
return data
This converts my Streaming Body into a json content, using a very hacky way. My output is in the following format. Output of
print(data)
{u'DATE_TIME': u'2017/06/19 16:24:41',
u'S3_PATH': u'S3://hw-app-a5e2ccf910/DocumentProcessingVA/WPI1/WPE2043/EntityTaggingFinalOutput/c209201-1c04-49c3-ab4d-a7cc79d7f18d.json'}
However when I try to print the value passing the key, I get a key error
print(data["DATE_TIME"])
If I do this in a for loop by iterating through the keys and then printing, it is printing the value fine. i.e doing this
for info in data:
print(data[info])
Is there a better way to do this, as I have larger JSON files, and would like to not execute a for loop every time to get only bits of data from it, when I should be able to use the key. I''m not sure if this is an encoding error or the way I have defined my function to get the JSON data.
P.S trying to load as a string from the Streaming Body output doesn't help