1

I have a json string that I want to parse into a dictionary using the built in json module. I'm able to do this using loads(), as follows:

>>> j = '''{
...   "first": 1,
...   "second": "two",
...   "third": {
...     "first": "one",
...     "second": null
...   }
... }'''
>>> d = json.loads(j)
>>> pprint(d)
{u'first': 1,
 u'second': u'two',
 u'third': {u'first': u'one',
            u'second': None}}

The issue is everything loads as a unicode. Is there a way to force python to load everything as a string instead? Alternatively, is there a simple way to do a deep convert once the dict is created.

NOTE: Because I know people will ask, it is safe to assume that the keys and values I get will contain only ASCII characters and symbols, so I will not have a problem with special characters.

ewok
  • 20,148
  • 51
  • 149
  • 254

1 Answers1

4

You could supply an object_hook or object_pairs_hook parameter to json.loads().

from pprint import pprint
import json

def str_hook(obj):
    return {k.encode('utf-8') if isinstance(k,unicode) else k :
            v.encode('utf-8') if isinstance(v, unicode) else v
            for k,v in obj}

j = '''{
  "first": 1,
  "second": "two",
  "third": {
    "first": "one",
    "second": null
  }
}'''
d = json.loads(j, object_pairs_hook=str_hook)
pprint(d)
Robᵩ
  • 163,533
  • 20
  • 239
  • 308