Json with comments published: 12/01/23 (dd/mm/yy) updated: nope I just read https://ruudvanasseldonk.com/2023/01/11/the-yaml-document-from-hell, and it made me think about something. > Json documents double as valid Python literals with minimal adaptation, and Python supports trailing > commas and comments. Yes you can add comments in a Python dict that you'll export as json! So I tested something, and voilà! Here's my code for having a json file with comments: ``` import json;print(json.dumps({ "key": "val", "another key": 42, # this is a valid comment "this is a list": [ { "key in list": 1, "another": 2, # can this improve readability of the document? "third": 3 }, {} # an empty dict? no problem it's valid json ], # LOOK MA NO ERROR ON TRAILING COMMA UNLIKE REGULAR JSON })) ``` You just need to replace the first "{" by "import json;print(json.dumps({" and the last "}" by "}))", and maybe to rename the file to something like config.json.py in order to remember that the file is actually some really simple Python script. Run it like this: ``` $ python3 config.json.py {"key": "val", "another key": 42, "this is a list": [{"key in list": 1, "another": 2, "third": 3}, {}]} ``` ---- Want to indent your json? Add "indent=X" in your file, near the end! ``` import json;print(json.dumps({ "key": "val", "another key": 42, # this is a valid comment "this is a list": [ { "key in list": 1, "another": 2, # can this improve readability of the document? "third": 3 }, {} # an empty dict? no problem it's valid json ], # LOOK MA NO ERROR ON TRAILING COMMA UNLIKE REGULAR JSON }, indent=2)) ``` ``` $ python3 config.json.py { "key": "val", "another key": 42, "this is a list": [ { "key in list": 1, "another": 2, "third": 3 }, {} ] } ```