On Apr 5, 5:00 am, Steven D'Aprano <steve +comp.lang.pyt...@pearwood.info> wrote: > 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.
I've been bitten by this gotcha too. Maybe "boggled my brain" would be a bit of hyperbole, but it did cause me minor pain, and brief but frustrating pain is the whole point of "gotcha" presentations. > It especially boggled my brain when I > saw the pathetically useless error message generated: > > [...] > ValueError: Expecting property name: line 1 column 1 (char 1) > > "Expecting property name"??? WTF??? I agree with you that the error message is pretty puzzling. I can understand the rationale of the parser authors not to go overboard with diagnosing these errors correctly to users, since it would complicate the parser code and possibly slow it down even for well formed JSON. On the other hand, I think that parsers can distinguish themselves by anticipating the most common gotchas and giving clear messages. > 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. > Yep. -- http://mail.python.org/mailman/listinfo/python-list