0

I'm trying to use SharePoint 2010's REST API, which was going swimmingly until i ran into this:

Traceback (most recent call last):
  File "TestJSON.py", line 21, in <module>
    json.loads(s)
  File "c:\Python33\lib\json\__init__.py", line 316, in loads
    return _default_decoder.decode(s)
  File "c:\Python33\lib\json\decoder.py", line 351, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "c:\Python33\lib\json\decoder.py", line 367, in raw_decode
    obj, end = self.scan_once(s, idx)
ValueError: Expecting ',' delimiter: line 1 column 14 (char 13)

Test case:

import json
s='''{"etag": "W/\"1\""}'''
json.loads(s)

Python 3.3.5 gives the same error. Did i find a bug in the JSON library?

Update:

The actual error i'm getting (preceded by affected part) is this:

>>>>>>Err:tration?$filter=Modified%20gt%20datetime\'2014-04-30T00:00:00.000Z\'&$orderby=Mo<<<<<<<
Traceback (most recent call last):
  File "TestURL.py", line 41, in <module>
    j = json.loads(body)
  File "c:\Python33\lib\json\__init__.py", line 316, in loads
    return _default_decoder.decode(s)
  File "c:\Python33\lib\json\decoder.py", line 351, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "c:\Python33\lib\json\decoder.py", line 367, in raw_decode
    obj, end = self.scan_once(s, idx)
ValueError: Invalid \escape: line 4005 column 114 (char 314020)

from

body = response.read().decode("utf-8")
print(">>>>>>Err:{}<<<<<<<".format(body[314020-40:314020+40]))
Cees Timmerman
  • 13,202
  • 8
  • 78
  • 106

2 Answers2

3

The string literal is not escaped correctly. Make sure the string really represent JSON.

>>> s = r'''{"etag": "W/\"1\""}'''  # NOTE: raw string literal
>>> json.loads(s)
{'etag': 'W/"1"'}
falsetru
  • 314,667
  • 49
  • 610
  • 551
  • 1
    According to [this answer](http://stackoverflow.com/a/3020108/819417), MicrosoftSharePointTeamServices 14.0.0.6029's JSON output is incorrect. – Cees Timmerman Jun 24 '14 at 15:22
1

The \' sequence is invalid JSON. Single quotes do not need to be escaped, making this an invalid string escape.

You could try to repair it after the fact:

import re

data = re.sub(r"(?<!\\)\\'", "'", data)

before loading it with JSON. This replaces \' with plain ', provided the backslash wasn't already escaped by a preceding \.

Since single quotes can only appear in string values, this should be safe.

Martijn Pieters
  • 889,049
  • 245
  • 3,507
  • 2,997