[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Hack for JSON sequences with trailing commas?
From: |
Barry Margolin |
Subject: |
Re: Hack for JSON sequences with trailing commas? |
Date: |
Wed, 01 Aug 2018 02:53:23 -0400 |
User-agent: |
MT-NewsWatcher/3.5.3b3 (Intel Mac OS X) |
In article <mailman.4482.1533056121.1292.help-gnu-emacs@gnu.org>,
Skip Montanaro <skip.montanaro@gmail.com> wrote:
> I've been using programming languages where sequences can contain
> trailing commas since my earliest days with C back in the early 80s.
> Of course, Python (the language in which I write most things these
> days) supports that. As does JavaScript, though for some crazy reason,
> not if that JavaScript is just JSON. This is a valid JSON input
> string:
>
> "[1,2,3]"
>
> but attempts to decode this string using Python's json.loads()
> function provoke a JSONDecodeError:
>
> "[1,2,3,]"
>
> *sigh*
>
> I suppose if I was to dig enough I'd find a reason JSON's author
> thought that was a good idea.
JSON is a very limited subset of Javascript notation for literals. Many
things that are allowed in Javascript source code are not allowed in
JSON. The reason is presumably to simplify the design of JSON parsers --
they don't have to deal with all possible input formats.
For instance, Javascript allows both single and double quotes to be used
as string delimiters, but JSON only allows double quotes. Javascript
allows object keys to be written without quoting them if they're valid
JS names, JSON requires all keys to be quoted. And JS allows trailing
commas, JSON doesn't.
--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
- Re: Hack for JSON sequences with trailing commas?,
Barry Margolin <=