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).
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 |
None
|
object_pairs_hook
|
Any
|
Called with an ordered list of pairs. |
None
|
**kwargs
|
Any
|
Additional keyword arguments passed to :func: |
{}
|
Returns:
| Type | Description |
|---|---|
Any
|
Deserialized Python object. |
Raises:
| Type | Description |
|---|---|
JSONCDecodeError
|
If the text is not valid JSONC. |
Source code in jsonc/jsonc.py
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 |
None
|
object_pairs_hook
|
Any
|
Called with an ordered list of pairs. |
None
|
**kwargs
|
Any
|
Additional keyword arguments passed to :func: |
{}
|
Returns:
| Type | Description |
|---|---|
Any
|
Deserialized Python object. |
Raises:
| Type | Description |
|---|---|
JSONCDecodeError
|
If the content is not valid JSONC. |
Source code in jsonc/jsonc.py
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 |
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: |
{}
|
Returns:
| Type | Description |
|---|---|
str
|
JSON string. |
Source code in jsonc/jsonc.py
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 |
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: |
{}
|