json.loads() is returning a unicode object instead of a dictionary

Dr Potato picture Dr Potato · Mar 24, 2014 · Viewed 16.5k times · Source

I'm reading in json from a file on a remote server using fabric:

from StringIO import StringIO

output = StringIO()
get(file_name, output)

output = output.getvalue()

The value of output is now:

'"{\\n \\"status\\": \\"failed\\", \\n \\"reason\\": \\"Record already exists.\\"\\n}"'

When I try to parse this string to a dictionary using json.loads(output) its returns the unicode object u'{\n "status": "failed", \n "reason": "Record already exists."\n}' rather than a dictionary.

I've come up with a fairly bad fix, just passing the new unicode object back into json.loads():

json.loads(json.loads(output))

Is there any way other solution to this?

Cheers

Answer

m.wasowski picture m.wasowski · Mar 24, 2014

Your data is escaped.

json.loads(output.decode('string-escape').strip('"'))

should give you desired results:

Out[12]: {'reason': 'Record already exists.', 'status': 'failed'}