Valid JSON giving JSONDecodeError: Expecting , delimiter

PythonJsonEscapingRawstring

Python Problem Overview


I'm trying to parse a json response data from youtube api but i keep getting an error.

Here is the snippet where it choking:

data = json.loads("""{ "entry":{ "etag":"W/\"A0UGRK47eCp7I9B9WiRrYU0.\"" } }""")

..and this happens:

JSONDecodeError: Expecting , delimiter: line 1 column 23 (char 23)

I've confirmed that it's valid json and I have no control over the formatting of it so how can I get past this error?

Python Solutions


Solution 1 - Python

You'll need a r before """, or replace all \ with \\. This is not something you should care about when read the json from somewhere else, but something in the string itself.

data = json.loads(r"""{ "entry":{ "etag":"W/\"A0UGRK47eCp7I9B9WiRrYU0.\"" } }""")

see here for more information

Solution 2 - Python

You need to add r before your json string.

>>> st = r'{ "entry":{ "etag":"W/\"A0UGRK47eCp7I9B9WiRrYU0.\"" } }'
>>> data = json.loads(st)
>>>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionuserBGView Question on Stackoverflow
Solution 1 - PythonFelix YanView Answer on Stackoverflow
Solution 2 - PythonRanRagView Answer on Stackoverflow