Skip to content

JSONC API Reference

Auto-generated API documentation for the JSONC parser module.

jsonc

JSONC (JSON with Comments) parser — zero dependencies, stdlib only, Python 3.10+.

Part of zerodep: https://github.com/Oaklight/zerodep Copyright (c) 2026 Peng Ding. MIT License.

Drop-in replacement for commentjson / stdlib json with JSONC support.

Supports
  • Single-line comments: // and #
  • Block comments: /* ... */
  • Trailing commas in objects and arrays
  • All standard JSON types

Example::

loads('{"a": 1, // comment

"b": 2}') # {'a': 1, 'b': 2} load(open("config.jsonc")) # {...}

JSONCDecodeError

Bases: JSONDecodeError

Error raised when JSONC parsing fails.

Provides line and column numbers relative to the original JSONC source (before comment/trailing-comma stripping).

Source code in jsonc/jsonc.py
class JSONCDecodeError(json.JSONDecodeError):
    """Error raised when JSONC parsing fails.

    Provides line and column numbers relative to the original JSONC source
    (before comment/trailing-comma stripping).
    """

loads(text, *, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kwargs)

Deserialize a JSONC string to a Python object.

Strips //, #, and /* */ comments and trailing commas before delegating to :func:json.loads.

Parameters:

Name Type Description Default
text str

JSONC source string.

required
cls type[JSONDecoder] | None

Custom JSON decoder class.

None
object_hook Any

Called with the result of any object literal decoded.

None
parse_float Any

Called with every JSON float string decoded.

None
parse_int Any

Called with every JSON int string decoded.

None
parse_constant Any

Called with -Infinity, Infinity, NaN.

None
object_pairs_hook Any

Called with an ordered list of pairs.

None
**kwargs Any

Additional keyword arguments passed to :func:json.loads.

{}

Returns:

Type Description
Any

Deserialized Python object.

Raises:

Type Description
JSONCDecodeError

If the text is not valid JSONC.

Source code in jsonc/jsonc.py
def loads(
    text: str,
    *,
    cls: type[json.JSONDecoder] | None = None,
    object_hook: Any = None,
    parse_float: Any = None,
    parse_int: Any = None,
    parse_constant: Any = None,
    object_pairs_hook: Any = None,
    **kwargs: Any,
) -> Any:
    """Deserialize a JSONC string to a Python object.

    Strips ``//``, ``#``, and ``/* */`` comments and trailing commas before
    delegating to :func:`json.loads`.

    Args:
        text: JSONC source string.
        cls: Custom JSON decoder class.
        object_hook: Called with the result of any object literal decoded.
        parse_float: Called with every JSON float string decoded.
        parse_int: Called with every JSON int string decoded.
        parse_constant: Called with ``-Infinity``, ``Infinity``, ``NaN``.
        object_pairs_hook: Called with an ordered list of pairs.
        **kwargs: Additional keyword arguments passed to :func:`json.loads`.

    Returns:
        Deserialized Python object.

    Raises:
        JSONCDecodeError: If the text is not valid JSONC.
    """
    cleaned = _preprocess(text)
    try:
        return json.loads(
            cleaned,
            cls=cls,
            object_hook=object_hook,
            parse_float=parse_float,
            parse_int=parse_int,
            parse_constant=parse_constant,
            object_pairs_hook=object_pairs_hook,
            **kwargs,
        )
    except json.JSONDecodeError as exc:
        line, col = _remap_error_position(text, cleaned, exc.pos)
        raise JSONCDecodeError(exc.msg, text, exc.pos) from None

load(fp, *, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kwargs)

Deserialize a JSONC file to a Python object.

Parameters:

Name Type Description Default
fp IO[str]

A text file-like object containing JSONC.

required
cls type[JSONDecoder] | None

Custom JSON decoder class.

None
object_hook Any

Called with the result of any object literal decoded.

None
parse_float Any

Called with every JSON float string decoded.

None
parse_int Any

Called with every JSON int string decoded.

None
parse_constant Any

Called with -Infinity, Infinity, NaN.

None
object_pairs_hook Any

Called with an ordered list of pairs.

None
**kwargs Any

Additional keyword arguments passed to :func:json.loads.

{}

Returns:

Type Description
Any

Deserialized Python object.

Raises:

Type Description
JSONCDecodeError

If the content is not valid JSONC.

Source code in jsonc/jsonc.py
def load(
    fp: IO[str],
    *,
    cls: type[json.JSONDecoder] | None = None,
    object_hook: Any = None,
    parse_float: Any = None,
    parse_int: Any = None,
    parse_constant: Any = None,
    object_pairs_hook: Any = None,
    **kwargs: Any,
) -> Any:
    """Deserialize a JSONC file to a Python object.

    Args:
        fp: A text file-like object containing JSONC.
        cls: Custom JSON decoder class.
        object_hook: Called with the result of any object literal decoded.
        parse_float: Called with every JSON float string decoded.
        parse_int: Called with every JSON int string decoded.
        parse_constant: Called with ``-Infinity``, ``Infinity``, ``NaN``.
        object_pairs_hook: Called with an ordered list of pairs.
        **kwargs: Additional keyword arguments passed to :func:`json.loads`.

    Returns:
        Deserialized Python object.

    Raises:
        JSONCDecodeError: If the content is not valid JSONC.
    """
    return loads(
        fp.read(),
        cls=cls,
        object_hook=object_hook,
        parse_float=parse_float,
        parse_int=parse_int,
        parse_constant=parse_constant,
        object_pairs_hook=object_pairs_hook,
        **kwargs,
    )

dumps(obj, *, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, sort_keys=False, **kwargs)

Serialize a Python object to a JSON string.

This is a pass-through to :func:json.dumps for API compatibility.

Parameters:

Name Type Description Default
obj Any

Python object to serialize.

required
skipkeys bool

Skip keys that are not basic types.

False
ensure_ascii bool

Escape non-ASCII characters.

True
check_circular bool

Check for circular references.

True
allow_nan bool

Allow NaN, Infinity, -Infinity.

True
cls type[JSONEncoder] | None

Custom JSON encoder class.

None
indent int | str | None

Indentation level for pretty-printing.

None
separators tuple[str, str] | None

Item and key separators.

None
default Any

Called for objects that are not serializable.

None
sort_keys bool

Sort dictionary keys.

False
**kwargs Any

Additional keyword arguments passed to :func:json.dumps.

{}

Returns:

Type Description
str

JSON string.

Source code in jsonc/jsonc.py
def dumps(
    obj: Any,
    *,
    skipkeys: bool = False,
    ensure_ascii: bool = True,
    check_circular: bool = True,
    allow_nan: bool = True,
    cls: type[json.JSONEncoder] | None = None,
    indent: int | str | None = None,
    separators: tuple[str, str] | None = None,
    default: Any = None,
    sort_keys: bool = False,
    **kwargs: Any,
) -> str:
    """Serialize a Python object to a JSON string.

    This is a pass-through to :func:`json.dumps` for API compatibility.

    Args:
        obj: Python object to serialize.
        skipkeys: Skip keys that are not basic types.
        ensure_ascii: Escape non-ASCII characters.
        check_circular: Check for circular references.
        allow_nan: Allow ``NaN``, ``Infinity``, ``-Infinity``.
        cls: Custom JSON encoder class.
        indent: Indentation level for pretty-printing.
        separators: Item and key separators.
        default: Called for objects that are not serializable.
        sort_keys: Sort dictionary keys.
        **kwargs: Additional keyword arguments passed to :func:`json.dumps`.

    Returns:
        JSON string.
    """
    return json.dumps(
        obj,
        skipkeys=skipkeys,
        ensure_ascii=ensure_ascii,
        check_circular=check_circular,
        allow_nan=allow_nan,
        cls=cls,
        indent=indent,
        separators=separators,
        default=default,
        sort_keys=sort_keys,
        **kwargs,
    )

dump(obj, fp, *, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, sort_keys=False, **kwargs)

Serialize a Python object to a JSON file.

This is a pass-through to :func:json.dump for API compatibility.

Parameters:

Name Type Description Default
obj Any

Python object to serialize.

required
fp IO[str]

A text file-like object to write to.

required
skipkeys bool

Skip keys that are not basic types.

False
ensure_ascii bool

Escape non-ASCII characters.

True
check_circular bool

Check for circular references.

True
allow_nan bool

Allow NaN, Infinity, -Infinity.

True
cls type[JSONEncoder] | None

Custom JSON encoder class.

None
indent int | str | None

Indentation level for pretty-printing.

None
separators tuple[str, str] | None

Item and key separators.

None
default Any

Called for objects that are not serializable.

None
sort_keys bool

Sort dictionary keys.

False
**kwargs Any

Additional keyword arguments passed to :func:json.dump.

{}
Source code in jsonc/jsonc.py
def dump(
    obj: Any,
    fp: IO[str],
    *,
    skipkeys: bool = False,
    ensure_ascii: bool = True,
    check_circular: bool = True,
    allow_nan: bool = True,
    cls: type[json.JSONEncoder] | None = None,
    indent: int | str | None = None,
    separators: tuple[str, str] | None = None,
    default: Any = None,
    sort_keys: bool = False,
    **kwargs: Any,
) -> None:
    """Serialize a Python object to a JSON file.

    This is a pass-through to :func:`json.dump` for API compatibility.

    Args:
        obj: Python object to serialize.
        fp: A text file-like object to write to.
        skipkeys: Skip keys that are not basic types.
        ensure_ascii: Escape non-ASCII characters.
        check_circular: Check for circular references.
        allow_nan: Allow ``NaN``, ``Infinity``, ``-Infinity``.
        cls: Custom JSON encoder class.
        indent: Indentation level for pretty-printing.
        separators: Item and key separators.
        default: Called for objects that are not serializable.
        sort_keys: Sort dictionary keys.
        **kwargs: Additional keyword arguments passed to :func:`json.dump`.
    """
    json.dump(
        obj,
        fp,
        skipkeys=skipkeys,
        ensure_ascii=ensure_ascii,
        check_circular=check_circular,
        allow_nan=allow_nan,
        cls=cls,
        indent=indent,
        separators=separators,
        default=default,
        sort_keys=sort_keys,
        **kwargs,
    )