0

I'm loading an array from a bucket content in S3, but when I load the array and printed the string using the following code:

for obj_array in bucket_array.objects.all():
  json_array = obj_array.get()['Body'].read()

it has the u character at the beginning of each value like follows:

current array:

{
  u'values': [{
    u'sensor': u'2',
    u'temperature': u'24.5',
    u'unit': u 'celsius',
    u'datetime': u'23-02-2017 21:03:50'
  }, {
    u'sensor': u'2',
    u'temperature': u'24.5',
    u'unit': u'celsius',
    u'datetime': u'24-02-2017 4:00:36'
  }]
}

So, when I tried to do array_data = json.loads(json_array) it throws me the following error:

Expecting property name: line 1 column 2 (char 1): ValueError
Any ideas how to solve this?
Community
  • 1
  • 1
Luis Diego Rojas
  • 182
  • 1
  • 11
  • Can you use `ctrl - k` to format your code properly? It is hard to read what your code is doing right now. – Chirag Feb 24 '17 at 04:17
  • use json.dumps(json_array) to convert your son to python dict! – Keerthana Prabhakaran Feb 24 '17 at 04:31
  • The 'u' character only means that it is a unicode formatted. – Chirag Feb 24 '17 at 05:04
  • You are having a Python (2) dictionary already, not JSON. the `u` marks a Python 2 unicode string. BTW single quotation marks are also disallowed in JSON. – Klaus D. Feb 24 '17 at 05:11
  • I don't think you need the `json.loads()`, it appears you already have a python data structure. – Stephen Rauch Feb 24 '17 at 05:25
  • 1
    Possible duplicate of [Fastest way to convert a dict's keys & values from \`unicode\` to \`str\`?](http://stackoverflow.com/questions/1254454/fastest-way-to-convert-a-dicts-keys-values-from-unicode-to-str) – Stephen Rauch Feb 24 '17 at 05:25

0 Answers0