On Thu, 05 Apr 2012 11:06:11 +0000, Duncan Booth wrote: > Steven D'Aprano <steve+comp.lang.pyt...@pearwood.info> wrote: > >> JSON expects double-quote marks, not single: >> v = json.loads("{'test':'test'}") fails v = >> json.loads('{"test":"test"}') succeeds >> >> > You mean JSON expects a string with valid JSON? Quelle surprise.
No. The surprise is that there exists a tool invented in the 21st century that makes a distinction between strings quoted with " and those quoted with '. Being used to a sensible language like Python, it boggled my brain the first time I tried to write some JSON and naturally treated the choice of quote mark as arbitrary. It especially boggled my brain when I saw the pathetically useless error message generated: py> json.loads("{'test':'test'}") Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/local/lib/python3.2/json/__init__.py", line 307, in loads return _default_decoder.decode(s) File "/usr/local/lib/python3.2/json/decoder.py", line 351, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "/usr/local/lib/python3.2/json/decoder.py", line 367, in raw_decode obj, end = self.scan_once(s, idx) ValueError: Expecting property name: line 1 column 1 (char 1) "Expecting property name"??? WTF??? The reason this is a Gotcha rather than a bug is because the JSON standard specifies the behaviour (probably in order to be compatible with Javascript). Hence, although the behaviour is mind-numbingly stupid, it is deliberate and not a bug. Hence, a gotcha. -- Steven -- http://mail.python.org/mailman/listinfo/python-list