I kept following JSON in S3 bucket 'test'
{
'Details' : "Something"
}
I am using following code to read this JSON and printing the key 'Details'
s3 = boto3.resource('s3',
aws_access_key_id=<access_key>,
aws_secret_access_key=<secret_key>
)
content_object = s3.Object('test', 'sample_json.txt')
file_content = content_object.get()['Body'].read().decode('utf-8')
json_content = json.loads(repr(file_content))
print(json_content['Details'])
And i am getting error as 'string indices must be integers' I don't want to download the file from S3 and then reading..
As mentioned in the comments above, repr
has to be removed and the json
file has to use double quotes for attributes. Using this file on aws/s3:
{
"Details" : "Something"
}
and the following Python code, it works:
import boto3
import json
s3 = boto3.resource('s3')
content_object = s3.Object('test', 'sample_json.txt')
file_content = content_object.get()['Body'].read().decode('utf-8')
json_content = json.loads(file_content)
print(json_content['Details'])
# >> Something