سؤال

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)
هل كانت مفيدة؟

المحلول

Your json is not valid, your errors are:

  1. you have an extra bracket, it shouldn't be there;
  2. you are using single quotes, instead you should use double quotes;
  3. and you are missing commas at the end of each row.

Pay attention on your errors here:

[
    {
        '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
]

It should be like this:

[
    {
        "address": "Bramalea, L6T 0E2",
        "type": "home"
    },
    {
        "address": "A, 46 Peel Drive, ASDF23",
        "type": "office"
    }
]

Here is the final result for 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'}]
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top