Reputation: 13
How can I read a file with json which comes from Amazon S3 and convert to a list?
The file contains:
[{
'address': 'Bramalea, L6T 0E2'
'type': 'home'
}, {
'address': 'A, 46 Peel Drive, ASDF23'
'type': 'office'
}
}]
I tried:
conn = S3Connection(AWS_KEY, AWS_SECRET)
bucket = conn.get_bucket(BUCKET_NAME)
for key in bucket.list(DIR_Name):
data = key.get_contents_as_string()
print json.loads(data)
But it's raising :
print json.loads(data)
File "/usr/lib/python2.7/json/__init__.py", line 326, in loads
return _default_decoder.decode(s)
File "/usr/lib/python2.7/json/decoder.py", line 366, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python2.7/json/decoder.py", line 382, in raw_decode
obj, end = self.scan_once(s, idx)
ValueError: Expecting property name: line 2 column 5 (char 8)
Upvotes: 0
Views: 4239
Reputation: 10471
Your json is not valid, your errors are:
[
{
'address': 'Bramalea, L6T 0E2' <-- missing comma and using single quotes
'type': 'home' <-- using single quotes
},
{
'address': 'A, 46 Peel Drive, ASDF23' <-- missing comma and using single quotes
'type': 'office' <-- using single quotes
}
} <---- extra bracket here
]
[
{
"address": "Bramalea, L6T 0E2",
"type": "home"
},
{
"address": "A, 46 Peel Drive, ASDF23",
"type": "office"
}
]
json.loads(data)
with the correct json:[{u'type': u'home', u'address': u'Bramalea, L6T 0E2'}, {u'type': u'office', u'address': u'A, 46 Peel Drive, ASDF23'}]
Upvotes: 1