3

The comment are causing errors. I have a contents.json file which looks like:

{
"Fridge": [
    ["apples"],
    ["chips","cake","10"]    // This comment here is causing error
],
"car": [
    ["engine","tires","fuel"],
    ]
}

My python script is like this

import json
jsonfile = open('contents.json','r')
jsondata = jsonfile.read()
    
objec = json.loads(jsondata)

list_o = objec['Fridge']

for i in (list_o):
    print(i)

In my list_o, i am trying to load Fridge from contents.jsonfile, when JSON file has that comment, it gives me an error, when the JSON file doesn't have the comment, the script runs properly.

I understand that comments is not proper JSON format, but is there any way to ignore comments of JSON file?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Zadmain
  • 35
  • 1
  • 3

5 Answers5

5

@NielGodfreyPonciano's answer would work most of the time but would fail when // is part of a string.

For a more robust solution you can parse it as JSON5, a superset of JSON that supports comments, with the pyjson5 module:

import json5

data = '''{
"Fridge": [
    ["apples"],
    ["chips","cake","10"]    // This comment here is causing error
],
"car": [
    ["engine","tires","fuel"],
    ]
}'''

print(json5.loads(data))

This outputs:

{'Fridge': [['apples'], ['chips', 'cake', '10']], 'car': [['engine', 'tires', 'fuel']]}

Demo: https://replit.com/@blhsing/MicroHatefulEnterprise

blhsing
  • 91,368
  • 6
  • 71
  • 106
1

Read the file per line and remove the comment part.

import json

jsondata = ""
with open('contents.json', 'r') as jsonfile:
    for line in jsonfile:
        jsondata += line.split("//")[0]

objec = json.loads(jsondata)

list_o = objec['Fridge']

for i in (list_o):
    print(i)
['apples']
['chips', 'cake', '10']

Update

You can also easily just use a library such as commentjson. Just replace :

objec = json.loads(jsondata)

To

import commentjson  # python3 -m pip install commentjson
objec = commentjson.loads(jsondata)
  • 2
    While this may work most of the time, it's possible for it to fail depending on what's in the file. – martineau Sep 02 '21 at 00:59
  • @martineau, so far this works in this case, i was curious, in what case would this not work ? – Zadmain Sep 02 '21 at 01:20
  • 1
    It wouldn't work if you are using other comment styles such as `#` or if the real data in your json file contains a `//` character such as `["appl//es"],`. I updated the answer to use a library. It's more usable for your use case. – Niel Godfrey Pablo Ponciano Sep 02 '21 at 01:24
  • 2
    A fairly obvious example would be if a string in the JSON data contained a `//`. like `["engine","tires","fuel",'//oil']` — which is why I also said it was unlikely to be a problem. – martineau Sep 02 '21 at 01:25
  • Could be problematic if the JSON data is in a single line (not pretty printed) – Thyag Sep 02 '21 at 01:26
  • If it's in a single line, then it should be safe to assume that there will be no comments in the middle as that would totally invalidate the json e.g. `{"abc": "def" // Some comment here "ghi": "jkl"}`. So if someone just added that `// Some comment here` in the middle, I guess someone has to be fired :) – Niel Godfrey Pablo Ponciano Sep 02 '21 at 01:30
  • @Thyag: Those kind of comments, aka single-line or in-line style comments, start with `//` and continue until the end of the line, so it's very unlikely they would be in JSON data put all on a single line for obvious reasons. – martineau Sep 02 '21 at 01:32
  • @Niel: Using a module like commentjson would probably be the best approach because ultimately what's needed is something that will actually *parse* the data in the file in order to separate them out — assuming that is syntactically possible. – martineau Sep 02 '21 at 01:36
  • @martineau yes agree. And as you pointed out already, my first solution is flawed and could fail depending on the contents of the json file. – Niel Godfrey Pablo Ponciano Sep 02 '21 at 01:38
  • Imagine that you're building a site like, say, StackOverflow, where you might want to return JSON responses that include code snippets. And oh, hey, one of those code snippets is C-style code that includes an embedded `//` comment... – jamesdlin Jun 22 '22 at 10:32
1

Following @blhsing's and @NielGodfreyPonciano's answers, you could also achieve that with only built-in modules by using regular expressions:

import re
import json


with open("contents.json", "r") as JSONfile:
    objec = json.loads("".join(re.split(r"(?://|#).*(?=\n)", JSONfile.read())).strip())
David Camp
  • 322
  • 2
  • 9
0

For me, the following works for reading JSONC in Python, with using re:

    def parseJsonc(text):
        text_without_comment = re.sub(r'\/\*(\*(?!\/)|[^*])*\*\/|\/\/.*', '', text)
        return json.loads(text_without_comment)

View here.

Jason
  • 941
  • 1
  • 10
  • 19
-3

"Errors due to comments" is precisely what should happen because JSON documents should NOT contain comments. You will have to fix the data at the source before consuming. In other words, if your data contains javascript-style comments, then it is not a JSON anymore (just a random non-standard text file)

At the consuming side, any attempts to remove comments could be unsafe as JSON documents do not need to have newlines/pretty printed.

Thyag
  • 1,217
  • 13
  • 14
  • 2
    I don't think your "answer" is adding much to the discussion that either the OP already knows or someone else has already pointed out in a comment (or isn't obvious). – martineau Sep 02 '21 at 01:42