I think you can solve this with a regex, it works for me:
import re pattern = re.compile('"([a-zA-Z0-9]+)"\s*:\s*(".*"|\[.*\]|\{.*\})') dict(re.findall(pattern, json_string))
But I do not know if it is faster, you need to try to use your data.
[EDIT]
Yes, it's faster. I tried the scripts below, and the regex version is 5 times faster.
using json module:
import json val=''' { "key1": "val1", "key2": ["a","b", 3], "key3": {"foo": 27, "bar": [1, 2, 3]} } ''' for n in range(100000): dict((k,json.dumps(v)) for k,v in json.loads(val).items())
using regex:
import re val='''{ "key1": "val1", "key2": ["a","b", 3], "key3": {"foo": 27, "bar": [1, 2, 3]} }''' pattern = re.compile('"([a-zA-Z0-9]+)"\s*:\s*(".*"|\[.*\]|\{.*\})') for n in range(100000): dict(re.findall(pattern, val))
olivecoder
source share