help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Hack for JSON sequences with trailing commas?


From: Skip Montanaro
Subject: Hack for JSON sequences with trailing commas?
Date: Tue, 31 Jul 2018 11:58:52 -0500

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.

I'm using JSON as a config file format for some code (not as an
interchange format, so I could care less what Chrome or IE think about
it), so I'm actually writing it by hand. Emacs tells me "*.json" files
are JavaScript, so it's quite happy with trailing commas.I installed
json-mode, but it's also happy with trailing commas.

Is there some clever hack to coax Emacs into stripping those trailing
commas I love so much? Maybe a JSON write-file hook to strip them and
a read-file-hook to add them back?

For the moment, for sequences which extend across multiple lines, I've
sort of adopted preceding the second through n-th elements with commas
instead of appending commas to the first through n-1st elements.

Thanks,

Skip, missing his commas in Illinois...



reply via email to

[Prev in Thread] Current Thread [Next in Thread]