70

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?

smci
  • 32,567
  • 20
  • 113
  • 146
userBG
  • 6,860
  • 10
  • 31
  • 39

2 Answers2

90

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

Felix Yan
  • 14,841
  • 7
  • 48
  • 61
17

You need to add r before your json string.

>>> st = r'{ "entry":{ "etag":"W/\"A0UGRK47eCp7I9B9WiRrYU0.\"" } }'
>>> data = json.loads(st)
>>>
RanRag
  • 48,359
  • 38
  • 114
  • 167
  • 1
    I think it's better to use """ instead of ' to enclose the json because the data can have unescaped single quotes in it. Thanks for the answer. – userBG Feb 06 '12 at 06:50
  • @ofko: I know that I just wrote this for clarity. – RanRag Feb 06 '12 at 06:53