title: Parsing malformed JSON in JavaScript
body: <p>Thanks for looking!</p>
  <h1>BACKGROUND</h1>
  <p>I am writing some front-end code that consumes a JSON service which is returning malformed JSON.  Specifically, the keys are not surrounded with quotes:</p>
  <pre><code>{foo: &quot;bar&quot;}
  </code></pre>
  <p>I have NO CONTROL over the service, so I am correcting this like so:</p>
  <pre><code>var scrubbedJson = dirtyJson.replace(/(['&quot;])?([a-zA-Z0-9_]+)(['&quot;])?:/g, '&quot;$2&quot;: ');
  </code></pre>
  <p>This gives me well formed JSON:</p>
  <p>{&quot;foo&quot;: &quot;bar&quot;}</p>
  <h1>Problem</h1>
  <p>However, when I call <code>JSON.parse(scrubbedJson)</code>, I still get an error.  I suspect it may be because the entire JSON string is surrounded in double quotes but I am not sure.</p>
  <h1>UPDATE</h1>
  <p>This has been solved--the above code works fine.  I had a rogue single quote in the body of the JSON that was returned.  I got that out of there and everything now parses.  Thanks.
  Any help would be appreciated.</p>