A lightweight, native tool for parsing .jsonc files
Likely abandoned. No release or commit for years (last release over 5 years ago).
pip install jsonc-parserjsonc-parser
This package is a lightweight, zero-dependency module for parsing files with .jsonc extension. (a.k.a. JSON with comments)
Installation
To install this package, simply download it from PyPI:
pip install jsonc-parser
Also you can build it yourself from source code available on GitHub
Usage
You need to just import JsoncParser class from this package:
from jsonc_parser.parser import JsoncParser
This class requires no instance to function (i.e. it is fully static)
Functions
These are all methods that JsoncParser class provides for working with .jsonc files:
-
JsoncParser.parse_file(filepath: PathLike) -> dict
This function parses file, specified in filepath parameter, and deserializes it into a valid Python object (dictionary), removing any comment in the process. No alterations are made it the file itself. filepath parameter 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 raise FunctionParameterError if 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 raise FileError exception if file's format is unsupported and a ParserError exception if file cannot be parsed/contains invalid JSON data.
-
JsoncParser.parse_str(_string: str) -> dict
This function parses string, specified in _string parameter, 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 raise FunctionParameterError if _ parameter is not a string or is empty. Also this function will raise a exception if file cannot be parsed/contains invalid JSON data.