Python - Converting a string with escape characters to json
<p>JSON objects are printed into my syslog file. I need to extract the string from the log and convert it into JSON. I don't have any problems extracting the string between '<code>{</code>' and '<code>}</code>', but certain strings have an escape character in them, and this is causing <code>json.loads</code> to fail</p>

<p>Here is the problem:</p>

<pre><code>&gt;&gt;&gt; import json
&gt;&gt;&gt; resp = '{"from_hostname": {"value": "mysite.edu\"", "value2": 0, "value3": 1}}'
&gt;&gt;&gt; json.loads(resp)
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 338, in loads
    return _default_decoder.decode(s)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 365, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 381, in raw_decode
    obj, end = self.scan_once(s, idx)
ValueError: Expecting , delimiter: line 1 column 41 (char 40)
&gt;&gt;&gt; resp[40]
'"'
&gt;&gt;&gt; resp[41]
','
&gt;&gt;&gt; resp[39]
'"'
&gt;&gt;&gt;
</code></pre>

<p>When <code>json</code> sees the <code>\"</code> I guess it only sees <code>"</code> and assumes that the string is over, and it throws the delimiter error. </p>

<p>I tried replacing <code>\"</code> with <code>\\"</code> but that doesn't seem to work. </p>

<p>NOTE: The <code>\"</code> can occur at the beginning or end or in the middle of the string.</p>

<p>How do I get this working?</p>