Free online JSON to an image converter. Just load your JSON data structure and it will automatically get converted to a code screenshot. There are no ads, popups, or nonsense, just an awesome JSON screenshot creator. JSON (JavaScript Object Notation, pronounced / ˈ dʒ eɪ s ən /; also / ˈ dʒ eɪ ˌ s ɒ n /) is an open standard file format, and data interchange format, that uses human-readable text to store and transmit data objects consisting of attribute–value pairs and array data types (or any other serializable value).
If you’re having trouble adding comments to your JSON file, there’s a good reason: JSON doesn’t support comments.
“I removed comments from JSON because I saw people were using them to hold parsing directives, a practice which would have destroyed interoperability,” writes Douglas Crockford, who popularized the text-based data format.
However, there’s a workaround. And that’s what this article is about: how to add comments to your JSON file.
Add Data as Comments
A way to skirt around the comments issue is to add data to your JSON file that function as comments.
Let’s go through an example, starting with this information in our JSON file:
Now let’s add another key-value pair to serve as our comment, which you can see in the first line in the code below:
Here’s another example. This time, we use two underscores at the start and end of the key:
The underscores help to differentiate the comment from the rest of the data in our file.
A Word of Caution
There’s an important detail to keep in mind.
The comments we added to our JSON file are included in the JSON object. In other words, the comments are treated as data.
Here’s what we mean.
This is the code in our file,
data.json
:Now we’re going to read that data from the file,
read_comments.py
:The result includes our comment:
Json File Opener
We can even extract the comment's value from the JSON object:
this is my comment
:Keep in mind that the comment is only a comment in the eyes of the developer—not the computer.
A Different Type of Comment
This JSON commenting practice is different from comments in programming languages, like Python, which are typically ignored when the program runs.
When we run the Python program above, we get the letters in the word, “house.” But we don’t see the comment. It’s ignored.
Commenting Options
JSMin is another option to consider.
Json File Opener Software
It’s a tool that removes extra whitespace and comments from JavaScript files. But it also works on JSON files. JSMin removes comments from JSON files before they're parsed.
So there are options when it comes to commenting in JSON files. Although they're not perfect solutions, at least there are ways to include the documentation you need when you need it.
I write about learning to program and the best ways to go about it (amymhaddad.com).