{"id":2901,"library":"commentjson","title":"CommentJSON","description":"commentjson is a Python package that enables you to include Python-style (#) and JavaScript-style (//) comments within your JSON files. Its API closely mirrors the standard library's `json` module, providing `load`, `loads`, `dump`, and `dumps` functionalities. Currently at version 0.9.0, the library is actively maintained, with recent releases focusing on performance improvements and compatibility.","status":"active","version":"0.9.0","language":"en","source_language":"en","source_url":"https://github.com/vaidik/commentjson","tags":["json","comments","configuration","parser"],"install":[{"cmd":"pip install commentjson","lang":"bash","label":"Install with pip"}],"dependencies":[{"reason":"Used as the underlying parsing engine for robust comment handling.","package":"lark","optional":false}],"imports":[{"note":"The library is imported directly as 'commentjson', mirroring the standard 'json' module's import pattern.","symbol":"commentjson","correct":"import commentjson"}],"quickstart":{"code":"import commentjson\nimport os\n\n# Example JSON string with comments\njson_string_with_comments = '''{\n    \"name\": \"Vaidik Kapoor\", # Person's name\n    \"location\": \"Delhi, India\", // Person's location\n    # Section contains info about\n    // person's appearance\n    \"appearance\": {\n        \"hair_color\": \"black\",\n        \"eyes_color\": \"black\",\n        \"height\": \"6\"\n    }\n}'''\n\n# Deserialize JSON with comments\ndata = commentjson.loads(json_string_with_comments)\nprint(f\"Loaded data: {data}\")\n\n# Serialize Python object back to JSON (comments are not preserved by default in dumps/dump)\n# To demonstrate, let's modify and dump\ndata['appearance']['height'] = '6 ft'\noutput_json = commentjson.dumps(data, indent=4)\nprint(f\"\\nSerialized data (without original comments):\\n{output_json}\")\n\n# If you were to load from a file, you'd use commentjson.load\n# with open('config.jsonc', 'w') as f:\n#     f.write(json_string_with_comments)\n# with open('config.jsonc', 'r') as f:\n#     file_data = commentjson.load(f)\n# print(f\"\\nLoaded from file: {file_data}\")\n","lang":"python","description":"This quickstart demonstrates how to parse a JSON string containing both Python-style `#` and JavaScript-style `//` comments using `commentjson.loads`. It then shows how to serialize a Python dictionary back into a JSON string using `commentjson.dumps`, which behaves like the standard `json` module, meaning comments are typically discarded during serialization. The API is designed to be a drop-in replacement for the standard `json` library."},"warnings":[{"fix":"Upgrade to Python 2.7 or, preferably, Python 3 for compatibility.","message":"Python 2.6 support was officially dropped in version 0.8.0. Applications targeting Python 2.6 will not be compatible with this or newer versions.","severity":"breaking","affected_versions":">=0.8.0"},{"fix":"Ensure `commentjson` and `lark` are updated to their latest compatible versions. If issues persist, consider pinning `lark` to a known working version as specified by `commentjson`'s `requirements.txt` on GitHub.","message":"commentjson relies on the `lark` parser. Earlier versions (0.8.1, 0.8.3) experienced compatibility issues with specific `lark-parser` releases. While generally resolved, ensure your `lark` dependency is compatible if you encounter parsing errors after updates.","severity":"gotcha","affected_versions":">=0.8.1"},{"fix":"Understand that `commentjson` is for *reading* commented JSON. If comments need to be preserved across read/modify/write cycles, you would need a more sophisticated solution or a different configuration format like YAML or JSONC, or manually manage comments (which `commentjson` does not support for round-tripping comments).","message":"Standard JSON (per RFC 8259) does not officially support comments. Using `commentjson` allows you to *parse* JSON files with comments, but if you `dump` or `dumps` the data, the comments will be lost. Any other standard JSON parser will fail if fed a file containing comments.","severity":"gotcha","affected_versions":"all"},{"fix":"Upgrade to version 0.8.2 or newer to ensure consistent and correct unicode handling.","message":"Version 0.8.2 fixed unicode handling issues to align with the standard `json` package. Older versions might have subtle differences in how unicode characters are processed.","severity":"gotcha","affected_versions":"<0.8.2"},{"fix":"Upgrade to version 0.9.0 or newer to use trailing commas. Remember that this is still non-standard JSON, only supported by `commentjson`.","message":"Prior to v0.9.0, trailing commas in JSON were not officially supported and would cause parsing errors. While v0.9.0 added support, be aware that standard JSON does not permit trailing commas, so other parsers will still fail.","severity":"gotcha","affected_versions":"<0.9.0"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}