JSON doesn’t permit comments by design. As explained by its creator Douglas Crockford.

I removed comments from JSON because I saw people were using them to hold parsing directives, a practice which would have destroyed interoperability.

But he also stated that you can use external or built-in tools to pre-parse JSON files and remove any comments before the actual parsing takes place.

In this short article, we’ll see how you can remove comments from JSON files using Python code.

How to Read JSON Files with Python

First, we need to be able to read JSON files in our Python code:

import json

with open('example.json') as json_file:
    data = json.load(json_file)
        print(data)

How to Remove Comments from your JSON File

There are various workarounds used by developers to add comments to JSON files generally.

You can use JS-style comments (single-line // and multiline /* .. */) in your JSON files and pre-parse them with your Python code to remove the comments before reading them in the previous way:

import json

with open('data.json', 'r') as jsonfile:
    jsondata = ''.join(line for line in jsonfile if not line.startswith('//'))
    data = json.loads(jsondata)

print(data)

You can also use external packages such as:

This is an example using:

import commentjson

with open('data.json', 'r') as file:
    ata = commentjson.load(file)

print(data)

This is another example from the docs:

>>> import commentjson
>>>
>>> json_string = """{
...     "name": "Vaidik Kapoor", # Person's name
...     "location": "Delhi, India", // Person's location
...
...     # Section contains info about
...     // person's appearance
...     "appearance": {
...         "hair_color": "black",
...         "eyes_color": "black",
...         "height": "6"
...     }
... }"""
>>>
>>> json_loaded = commentjson.loads(json_string)
>>> print json_loaded
{u'appearance': {u'eyes_color': u'black', u'hair_color': u'black', u'height': u'6'}, u'name': u'Vaidik Kapoor', u'location': u'Delhi, India'}