4

I have a json object that I am loading and replacing single with double quotes as I do. The syntax for this is:

response = json.loads(response.text.replace("'", '"'))

Within my data I have key/value pairs that take the format:

"name":"John O'Shea"

This is causing me to get the following traceback:

Traceback (most recent call last):
  File "C:\Python27\Whoscored\Test.py", line 204, in <module>
    response = json.loads(response.text.replace("'", '"').replace(',,', ','))
  File "C:\Python27\lib\json\__init__.py", line 338, in loads
    return _default_decoder.decode(s)
  File "C:\Python27\lib\json\decoder.py", line 366, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Python27\lib\json\decoder.py", line 382, in raw_decode
    obj, end = self.scan_once(s, idx)
ValueError: Expecting ',' delimiter: line 1 column 7751 (char 7750)

I don't actually want to replace the apostrophe in a name such as the one above, but I would have thought that my json.loads statement would have converted my key/value pair to this:

"name":"John O"Shea"

I'm assuming this would also fail however. What I need to know is:

1) Why is my json.loads statement not replacing the apostrophes in my string during the load? 2) What is the best way to escape the apostrophes within my string so that they do not cause an error, but are still displayed in the load?

I have used a json tester on my larger to string to confirm that there are no other errors that would stop the object from working correctly, which there are not.

Thanks

gdogg371
  • 3,879
  • 14
  • 63
  • 107

2 Answers2

5

Json uses " as a formatting character, so response.text.replace("'", '"') is just corrupting the file. Json escapes quotes inside strings as \" so this should work:

response = json.loads(response.text.replace("'", '\\"'))
tdelaney
  • 73,364
  • 6
  • 83
  • 116
0

if your json doesn't take special characters better convert them to Unicode

private static String escapeNonAscii(String str) {

    StringBuilder retStr = new StringBuilder();
    for(int i=0; i<str.length(); i++) {
        int cp = Character.codePointAt(str, i);
        int charCount = Character.charCount(cp);
        if (charCount > 1) {
            i += charCount - 1; // 2.
            if (i >= str.length()) {
                throw new IllegalArgumentException("truncated unexpectedly");
            }
        }

        if (cp < 128) {
            retStr.appendCodePoint(cp);
        } else {
            retStr.append(String.format("\\u%x", cp));
        }
    }
    return retStr.toString();
}
Dmitriy
  • 5,525
  • 12
  • 25
  • 38