From time to time I’ve wanted to be able to comment out lines in a JSON file.
This typically comes up when I’m making a butt load of changes to package.json
and I want to keep a visual history as I’m tweaking things.
Just last week, it came up with as I wanted to have a placeholder in some JSON
snippets for a blog post.
Thing is, there’s no formal comment format for JSON files. In fact, you could go
as far as to say that the JSON by nature should be self commenting and that
attempting to hack in comments is a bit of a code smell.
No judgement here.
So how do we add a comment? Just as easy as adding //
properties in your file:
{
"//": "This is a dirty, dirty comment",
"//": [
"This is a comment that spans",
"multiple lines!"
]
}
Nothing to it really.
Fact is, you can just do whatever you want as long as whatever is reading in
your JSON doesn’t bark about it:
{
"// foo": "bar",
"/*": "another one",
"_var": "value",
"ETC": "et cetera",
"#": "this too!"
}
As mentioned, YMMV depending on what’s reading in the data. With regard to
npm
, the father of npm
has made public comments about using //
and his intent on never using that particular property.
Happy Hacking and Happy Halloween, y’all!