title: Handling lazy JSON in Python - 'Expecting property name'
body: <p>Using Pythons (2.7) 'json' module I'm looking to process various JSON feeds. Unfortunately some of these feeds do not conform with JSON standards - in specific some keys are not wrapped in double speech-marks ("). This is causing Python to bug out.</p>
  
  <p>Before writing an ugly-as-hell piece of code to parse and repair the incoming data, I thought I'd ask - is there any way to allow Python to either parse this malformed JSON or 'repair' the data so that it would be valid JSON?</p>
  
  <p>Working example</p>
  
  <pre><code>import json
  &gt;&gt;&gt; json.loads('{"key1":1,"key2":2,"key3":3}')
  {'key3': 3, 'key2': 2, 'key1': 1}
  </code></pre>
  
  <p>Broken example</p>
  
  <pre><code>import json
  &gt;&gt;&gt; json.loads('{key1:1,key2:2,key3:3}')
  Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
    File "C:\Python27\lib\json\__init__.py", line 310, in loads
      return _default_decoder.decode(s)
    File "C:\Python27\lib\json\decoder.py", line 346, in decode
      obj, end = self.raw_decode(s, idx=_w(s, 0).end())
    File "C:\Python27\lib\json\decoder.py", line 362, in raw_decode
      obj, end = self.scan_once(s, idx)
  ValueError: Expecting property name: line 1 column 1 (char 1)
  </code></pre>
  
  <p>I've written a small REGEX to fix the JSON coming from this particular provider, but I forsee this being an issue in the future. Below is what I came up with.</p>
  
  <pre><code>&gt;&gt;&gt; import re
  &gt;&gt;&gt; s = '{key1:1,key2:2,key3:3}'
  &gt;&gt;&gt; s = re.sub('([{,])([^{:\s"]*):', lambda m: '%s"%s":'%(m.group(1),m.group(2)),s)
  &gt;&gt;&gt; s
  '{"key1":1,"key2":2,"key3":3}'
  </code></pre>