{"id":7326,"library":"jsonc-parser","title":"JSONC Parser","description":"jsonc-parser is a lightweight, zero-dependency Python module designed for parsing `.jsonc` files, which are JSON files extended to support JavaScript-style comments. It allows developers to easily deserialize JSONC content into Python dictionaries, stripping out comments in the process. The library is currently at version 1.1.5 and is actively maintained with updates released on an as-needed basis rather than a strict schedule.","status":"active","version":"1.1.5","language":"en","source_language":"en","source_url":"https://github.com/NickolaiBeloguzov/jsonc-parser","tags":["json","jsonc","parser","configuration","comments"],"install":[{"cmd":"pip install jsonc-parser","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Requires Python 3.5 or newer.","package":"python","optional":false}],"imports":[{"note":"The primary class `JsoncParser` resides within the `jsonc_parser.parser` submodule, not directly under `jsonc_parser`.","wrong":"import jsonc_parser","symbol":"JsoncParser","correct":"from jsonc_parser.parser import JsoncParser"}],"quickstart":{"code":"import os\nfrom jsonc_parser.parser import JsoncParser\n\n# Create a dummy JSONC file for demonstration\njsonc_content = '''\n{\n  \"name\": \"Test Config\", // Application name\n  \"version\": \"1.0.0\", /* Current version */\n  \"settings\": {\n    \"debugMode\": true,\n    \"logLevel\": \"INFO\"\n  }\n}\n'''\n\nfile_path = \"./config.jsonc\"\nwith open(file_path, \"w\") as f:\n    f.write(jsonc_content)\n\ntry:\n    # Parse the .jsonc file\n    data = JsoncParser.parse_file(file_path)\n    print(\"Parsed data:\", data)\n\n    # Parse a JSONC string\n    jsonc_string = '{ \"message\": \"Hello, /* commented */ World!\" }'\n    string_data = JsoncParser.parse_str(jsonc_string)\n    print(\"Parsed string data:\", string_data)\n\nfinally:\n    # Clean up the dummy file\n    if os.path.exists(file_path):\n        os.remove(file_path)\n","lang":"python","description":"This quickstart demonstrates how to parse a `.jsonc` file and a JSONC string using the `JsoncParser` class. It creates a temporary `.jsonc` file, reads its content (including comments), and then parses a direct string, showcasing how comments are stripped to yield a standard Python dictionary."},"warnings":[{"fix":"Ensure `filepath` arguments are valid path-like objects (e.g., strings, `pathlib.Path` objects). The error message will explicitly state if the parameter is not path-like.","message":"In version 1.1.5, the `filepath` parameter in methods like `parse_file` was changed to expect `os.PathLike` types instead of just strings. While strings generally work as `PathLike` objects, explicitly passing non-string `os.PathLike` objects is now fully supported. Incorrect types will raise a `FunctionParameterError`.","severity":"breaking","affected_versions":">=1.1.5"},{"fix":"Ensure JSONC files strictly adhere to standard JSON syntax for everything except comments. Remove any trailing commas in objects or arrays before parsing if errors occur.","message":"The `jsonc-parser` library handles comments in JSON files. However, it does not explicitly state support for other non-standard JSON features like trailing commas, which are common in some JSONC implementations (e.g., VS Code's configuration files). Standard JSON does not allow trailing commas, and this parser likely adheres to that. Expect `ParserError` if trailing commas are present.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Implement robust error handling around `jsonc-parser` calls. Catch `FileError`, `FunctionParameterError`, and `ParserError` to gracefully manage issues like missing files, incorrect parameter types, or malformed JSONC data.","message":"Parsing methods (`parse_file`, `parse_str`) can raise specific exceptions for different issues: `FileError` for file access problems, `FunctionParameterError` for invalid arguments (e.g., non-path-like `filepath`), and `ParserError` for invalid JSONC content that cannot be parsed.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Ensure the package is installed with `pip install jsonc-parser`. Correct the import statement to `from jsonc_parser.parser import JsoncParser`.","cause":"The `jsonc-parser` package is either not installed or is imported incorrectly. The main class is within a submodule.","error":"ModuleNotFoundError: No module named 'jsonc_parser'"},{"fix":"Provide a valid path, for example, `JsoncParser.parse_file('path/to/file.jsonc')` or `JsoncParser.parse_file(pathlib.Path('path/to/file.jsonc'))`.","cause":"The `filepath` argument passed to `JsoncParser.parse_file()` is not a valid string, bytes object, or `os.PathLike` object.","error":"jsonc_parser.errors.FunctionParameterError: filepath parameter is not a path-like object (str, bytes object representing a path, or os.PathLike compliant) or is empty"},{"fix":"Review the `.jsonc` content for standard JSON syntax errors (excluding comments). Use a JSON linter to validate the content without comments if necessary.","cause":"The content of the `.jsonc` file or string is not valid JSON, even after comments are stripped. This can include syntax errors like missing braces, unquoted keys, or invalid values.","error":"jsonc_parser.errors.ParserError: file cannot be parsed/contains invalid JSON data"}]}