Comments in JSON?
September 16, 2008 – 8:09 amSome of my students have discovered that JSON doesn’t support comments — they’re not in the syntax diagram on the json.org home page or the RFC, and various discussion threads bemoan their absence. We’d like to use JSON both for data interchange and for specifying test fixtures; we could live without comments for the former if we had to, but it’s a real pain to (for example) have the encrypted version of a password in a test fixture, but not the plaintext password it was derived from. One possibility is to add a “comment” field to every data type that needs commenting (e.g., the dictionary that represents a User object would have “comment” as one of its keys), but then the mapping from JSON to object or database entry is no longer 1-1. How are other people dealing with this?
5 Responses to “Comments in JSON?”
Use YAML for the fixtures? JSON is a subset of YAML, so it shouldn’t be too much hassle.
By Jeffrey Gelens on Sep 16, 2008
Why store the test fixture in JSON? Store it in YAML (similar although much bigger syntax that allows comments) and turn it into JSON if/when needed.
By Dave Doyle on Sep 16, 2008
The original specification (a few years ago) of JSON had comments included in the definition. However, it was removed, with the argument that JSON is meant as a machine interchange format only… or something like that.
By Koen on Sep 16, 2008
a search turns up some wrong answers by people who think you can just use javascript comments, because they don’t realize JSON is more than just passing a string to eval().
You can hack comments by taking advantage of the looseness of parsers in basically taking the last value for any key in a hash. This means you can do stuff like this:
{”name”: “// The name of something”,
“name”: “foo”}
It is not pretty, but it works.
That being said, who the heck needs comments in JSON?
By Calvin Spealman on Sep 20, 2008
@Calvin: we’re using JSON to describe test fixtures, and want comments (a) just because, and (b) so that when we’ve got an encrypted password stored as ASCII in a user record, we can also include the unencrypted password.
By Greg Wilson on Sep 20, 2008