Using django-social-auth to grab user data from facebook, it is returning a list of dicts in a unicode string. For example, response.get(‘education’) for a user is returning:
u”[{u’school’: {u’id’: u’12345′,
u’name’: u’Joe Thiesman High’},
u’type’: u’High School’}, {u’school’:
{u’id’: u’23456′, u’name’: u’Joe
Montana University’}, u’type’:
u’College’}]”
I want to convert this from a string to a list where I can extract the data, but am struggling. An answer to a similar question (String to Dictionary in Python) advised using:
foo=json.loads(string)
but that fails because its a list with nested dicts, 1 for each school, rather than just a dictionary, and it seems to be getting confused. the error im getting is:
ValueError: Extra Data: line 1 column 73 – line 1 column 144
Originally, I was getting a ValueError: Expecting Property Name: line 1, column 2, until I used string.replace() to exchange the ” with ‘, and vice-versa. That did get rid of that error, but I mention in case that wasn’t the correct solution.
Take a look at the answer to this question :
Convert a String representation of a Dictionary to a dictionary?
The use of python’s
ast.literal_evalmight be very useful to you. It is also a lot safer to use thanevalbecause it only will evaluate python data literals (strings, tuples, etc…) but not executable code.See ast.literal_eval in the python docs.