3

I'm trying to decode a json of a dictionary with strings as keys. The result is a dictionary with unicode keys. What is the best way to decode to a dictionary with string keys? Better: how do I prevent that strings are decoded into unicode strings? Off course I can loop afterwards...

What happens:

>>> import simplejson
>>> simplejson.loads('{"bar":["baz", null, 1.0, 2]}')
{u'bar': [u'baz', None, 1.0, 2]}
>>> simplejson.loads('"bar"')
u'bar'

Desired behaviour:

>>> import simplejson
>>> simplejson.loads('{"bar":["baz", null, 1.0, 2]}', ...?)
{'bar': ['baz', None, 1.0, 2]}
>>> simplejson.loads('"bar"', ..?)
'bar'
2
  • Why can't you use Unicode strings? Commented Jul 16, 2010 at 7:42
  • Because I want to pass the dictionary as keyword parameters foo(**parsed). It gives a type error. Commented Jul 16, 2010 at 7:44

1 Answer 1

2

You can't. Encode the strings after loading. Or even better, fix the rest of the code so that it doesn't fall over when using unicode.

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.