I don't really understand why you want this. Your variable is a normal Python dict with normal Unicode strings, and they happen to be printed as u''
to distinguish them from bytestrings, but that shouldn't matter for using them.
If you want to save them as strings to read them as data later, JSON is a fine format for that. So no need to call request's .json()
function at all, just use the response's .text
attribute -- it's already JSON, after all.
Your try
>>> ast.literal_eval(json.dumps(c))
Fails because you first turn c
into JSON again, and then try to parse it as a Python literal. That doesn't work because Python isn't JSON; in particular one has null
and the other has None
.
So maybe you want to change the Unicode strings into bytestrings? Like by encoding them as UTF8, that might work:
def to_utf8(d):
if type(d) is dict:
result = {}
for key, value in d.items():
result[to_utf8(key)] = to_utf8(value)
return result
elif type(d) is unicode:
return d.encode('utf8')
else:
return d
Or something like that, but I don't know why you would need it.