1

I have a corrupted json, like following:

{'a': 'b', 'c': "abcd'efgh ijklm"}

Before using python json.loads, i need to do 2 things:

  1. replace single quote inside double quote with \'
  2. replace all other single quote, except \' with double quote

I am not sure how to do both steps. Please help.

9
  • 1
    Why do you need to replace ' by \' in double-quoted strings? Commented Jun 18, 2016 at 8:06
  • @melpomene I am not sure... if that is not required, please tell what is best way to convert this string, so that it can be loaded in json.loads Commented Jun 18, 2016 at 8:09
  • The other part you have to be careful about is double quotes in single-quoted strings: '"' needs to become "\"", not """. Commented Jun 18, 2016 at 8:19
  • yeah... but how to do that.. Commented Jun 18, 2016 at 8:21
  • 1
    How about reading each line and use ast.literal_eval to convert the string to dict? Commented Jun 18, 2016 at 8:34

1 Answer 1

2

Actually you can't use the json.loads function to deserialize your string because the string doesn't contain a valid JSON document. Instead, you should consider to use the ast.literal_eval function.

Demo:

In [25]: import ast

In [26]: with open('inputfile') as f:
   ....:     for d in map(ast.literal_eval, f):
   ....:         print(d)
   ....:         
{'c': "abcd'efgh ijklm", 'a': 'b'}
{'c': "abcdgfgfgg", 'a': 'b'}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.