0

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)
4
  • 1
    json is just a string. you need to decode it back to a native (e.g. python) data structure. Commented May 14, 2014 at 19:09
  • add print data.__class__ before the print json.loads(data) and see what it returns. Commented May 14, 2014 at 19:44
  • Your JSON is not valid, it's as simple as that. You're missing , at the end of the address lines, and single quotes (') are not valid string delimiters in JSON, only double quotes ("). Commented May 14, 2014 at 19:49
  • @user3638022 see my answer below. Commented May 14, 2014 at 20:20

1 Answer 1

1

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'}]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.