Package: jsonc-parser
jsonc-parserThis package is a lightweight, zero-dependency module for parsing files with .jsonc extension. (a.k.a. JSON with comments)InstallationTo install this package, simply download it fromPyPI:pip install jsonc-parserAlso you can build it yourself from source code available onGitHubUsageYou need to just importJsoncParserclass from this package:from jsonc_parser.parser import JsoncParserThis class requires no instance to function (i.e. it is fully static)FunctionsThese are all methods that JsoncParser class provides for working with .jsonc files:JsoncParser.parse_file(filepath: PathLike) -> dictThis function parses file, specified infilepathparameter, and deserializes it into a valid Python object (dictionary), removing any comment in the process. No alterations are made it the file itself.filepathparameter specifies path to .jsonc file.from jsonc-parser.parser import JsoncParser

file_path = "./data.jsonc"
# Content from 'data.jsonc' -> {"version": "1.0.0" /*This is my project's version*/}

data = JsoncParser.parse_file(file_path)

print(data)
# Output: {'version': '1.0.0'}This function can raiseFunctionParameterErrorif filepath parameter is not a path-like object (str, bytes object representing a path, or os.PathLike compliant) or is empty. Also this function will raiseFileErrorexception if file's format is unsupported and aParserErrorexception if file cannot be parsed/contains invalid JSON data.JsoncParser.parse_str(_string: str) -> dictThis function parses string, specified in _stringparameter, and deserializes it into a valid Python object (dictionary), removing any comment in the process.from jsonc-parser.parser import JsoncParser

json_string = """{"version": "1.0.0" /*This is my project's version*/}"""

data = JsoncParser.parse_str(json_string)

print(data)
# Output: {'version': '1.0.0'}This function can raiseFunctionParameterErrorif _stringparameter is not a string or is empty. Also this function will raise aParserErrorexception if file cannot be parsed/contains invalid JSON data.JsoncParser.convert_to_json(filepath: PathLike, remove_file: bool = False) -> NoneThis function converts file from .jsonc to .json format, removing any comments in the process. filepath parameter specifies path to file and remove_file parameter specifies if .jsonc file will be removed (deleted from hard drive) after conversion. If set to True, this function will delete .jsonc file leaving only .json file. Otherwise, both files are not deleted. This function can raiseFunctionParameterErroriffilepathparameter is not a path-like object or is empty or ifremove_fileparameter is not a boolean.JsoncParser.convert_to_jsonc(filepath: PathLike, remove_file: bool = False) -> NoneThis function converts file from .json to .jsonc format, enabling comment support. filepath parameter specifies path to file andremove_fileparameter specifies if .jsonc file will be removed (deleted from hard drive) after conversion. If set to True, this function will delete .jsonc file leaving only .json file. Otherwise, both files are not deleted.
This function can raiseFunctionParameterErroriffilepathparameter is not a path-like object or is empty or ifremove_fileparameter is not a boolean.ExceptionsThere are a total of 3 custom exceptions that jsonc-parser can raise during its runtime. To access the in your script, simply import them from jsonc_parser.errors module:from jsonc_parser.errors import FileError, IncorrectParameterError, ParserErrorExceptions:FileErrorThis exception indicates that there is a problem with selected file.FunctionParameterErrorThis exception indicates that some of function's parameters are invalid. They may have wrong type, have invalid values or be erroneous in some other way.ParserErrorThis exception indicates that file cannot be parsed. It can have wrong extension, invalid data, etc.