title: Unescape json string in python
body: <p>I am getting the following string from a log file and I want to remove the backslashes from the string.</p>
  
  <p><strong>String from file:</strong></p>
  
  <p>This is the exact string fro the log file, except for some sensitive info replaced with dummy values.</p>
  
  <pre><code>2017-08-17 17:29:49.249  ERROR org.foo.bar.logging.ApplicationLogger - APIError={"input":"{\"requestBody\":\"{\\\"request\\\":{\\\"drequests\\\":{\\\"items\\\":[{\\\"Description\\\":\\\"I would like to add an additional card.\\\",\\\"Fields\\\":{\\\"Field\\\":[{\\\"FieldName\\\":\\\"Severity\\\",\\\"FieldValue\\\":\\\"4\\\"},{\\\"FieldName\\\":\\\"Contact\\\",\\\"FieldValue\\\":\\\"Phone\\\"},{\\\"FieldName\\\":\\\"CallbackNumber\\\",\\\"FieldValue\\\":\\\"1 (123) 123456\\\"},{\\\"FieldName\\\":\\\"Version\\\",\\\"FieldValue\\\":\\\"11.1\\\"},{\\\"FieldName\\\":\\\"Language\\\",\\\"FieldValue\\\":\\\"English\\\"}]},\\\"Product\\\":\\\"Visa\\\",\\\"Subject\\\":\\\"Adding an addition card\\\",\\\"Serial_Number\\\":\\\"123456789\\\"}]},\\\"Email\\\":\\\"someone@gmail.com\\\",\\\"First_Name\\\":\\\"Foo\\\",\\\"Last_Name\\\":\\\"Bar\\\"}}\"}"}
  </code></pre>
  
  <p><strong>Python code</strong></p>
  
  <pre><code>str = read_from_file()
  print str.replace('\\"', '"')
  </code></pre>
  
  <p>I tried this line of code but it is not having any effect. How can I get rid of backslahses from the json string?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I tried the solution of recursively doing json.loads but that didn't remove the backslashes.</p>
  
  <p>Just to give a better context - I am not processing this json string as JSON, instead I am only writing this to another file so it is more readable to another human. Below is my complete code.</p>
  
  <pre><code>import re
  import json
  from tailf import tailf
  
  
  def parseRecursive(obj):
      if isinstance(obj, str):
          try:  # see whether it is JSON: if so, parse it
              obj = json.loads(obj)
          except json.JSONDecodeError:
              pass  # if not, leave it like it is
          if isinstance(obj, dict):  # perform recursion
              for prop, val in obj.items():
                  obj[prop] = parseRecursive(val)
      return obj
  
  
  for line in tailf("/var/log/test.log"):
      m = re.search('([\d\-:\s]+).*ERROR.*APIError=(.*)', line)
      if m is None:
          print "No match"
      else:
          print m.group(1)
          print parseRecursive(m.group(2));
  </code></pre>
  
  <p>When I run this script it will print the string and the backslashes are not removed at all also notice two <code>u'</code> character in the beginning of the second line.</p>
  
  <p><strong>Output</strong></p>
  
  <pre><code>2017-08-17 17:29:49
  {u'input': u'{"requestBody":"{\\"request\\":{\\"drequests\\":{\\"items\\":[{\\"Description\\":\\"I would like to add an additional card.\\",\\"Fields\\":{\\"Field\\":[{\\"FieldName\\":\\"Severity\\",\\"FieldValue\\":\\"4\\"},{\\"FieldName\\":\\"Contact\\",\\"FieldValue\\":\\"Phone\\"},{\\"FieldName\\":\\"CallbackNumber\\",\\"FieldValue\\":\\"1 (123) 123456\\"},{\\"FieldName\\":\\"Version\\",\\"FieldValue\\":\\"11.1\\"},{\\"FieldName\\":\\"Language\\",\\"FieldValue\\":\\"English\\"}]},\\"Product\\":\\"Visa\\",\\"Subject\\":\\"Adding an addition card\\",\\"Serial_Number\\":\\"123456789\\"}]},\\"Email\\":\\"someone@gmail.com\\",\\"First_Name\\":\\"Foo\\",\\"Last_Name\\":\\"Bar\\"}}"}'}
  </code></pre>
  
  <p><strong>UPDATE</strong></p>
  
  <p><strong>It was a silly mistake!</strong> I had to type case the oobject to string and then call replace function. The below code worked.</p>
  
  <pre><code>import re
  from tailf import tailf
  
  for line in tailf("/var/log/test.log"):
      m = re.search('([\d\-:\s]+).*ERROR.*APIError=(.*)', line)
      if m is None:
          print "No match"
      else:
          print m.group(1)
          encodedString = m.group(2) + ''
          print str(encodedString).replace('\\', '')
  </code></pre>