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.
pip install jsonc-parserVerified import paths — ran on the pinned version, not inferred.
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.
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.
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.
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.
Ensure the package is installed with `pip install jsonc-parser`. Correct the import statement to `from jsonc_parser.parser import JsoncParser`.
Provide a valid path, for example, `JsoncParser.parse_file('path/to/file.jsonc')` or `JsoncParser.parse_file(pathlib.Path('path/to/file.jsonc'))`.Review the `.jsonc` content for standard JSON syntax errors (excluding comments). Use a JSON linter to validate the content without comments if necessary.