json

Standard library module View source

Native JSON parsing and encoding with a tagged JSON value.

Provides load, loads, dump, dumps, JSONDecoder, JSONEncoder, and JSONDecodeError. Standard formatting options and non-finite number defaults follow Python.

import json

value = json.loads('{"items": [1, true, null]}')
number = value["items"][0].as_int()
value["items"].append("text")
print(json.dumps(value, indent=2))

Differences from Python:

  • Decoding returns JSON. Use kind, is_null(), and typed accessors as_bool(), as_int(), as_float(), as_str(); wrong kinds raise TypeError (as_float() also accepts integers). JSON() is null; JSON(value) converts supported native scalars, optionals, lists, tuples, and string-keyed dicts. JSON.array() and JSON.object() create empty containers.
  • Use values(), keys(), or items() instead of direct iteration. Containers support indexing and mutation; copies share storage. Scalars are immutable. Equality is structural and kind-sensitive (JSON(1) != JSON(1.0)); values are unhashable. str(value) serializes JSON, including quotes around strings.
  • Integers are signed 64-bit; oversized literals raise OverflowError. Fractional and exponent-form numbers use binary64.
  • Keys must be strings. skipkeys, cls, default, object_hook, object_pairs_hook, parse_int, parse_float, and parse_constant are unsupported; unsupported native types, including nested members, are compile-time errors.
  • Parsing/encoding use max_depth=512, raising RecursionError when exceeded, even with check_circular=False. Structural comparison also has a 512 limit.
  • I/O is not streaming: load() reads the whole stream; dump() writes a complete string to a text stream. iterencode() is not provided.
  • Decode-error locations use Unicode code points; trailing-comma diagnostics follow CPython 3.14.
class JSON
@tuple
class JSON

Two-word value with a packed tag and string metadata, plus scalar bits or a pointer.

Constructors 2
__new__
__new__()
__new__
__new__(value)
Properties 1
kind
@property
kind(self)
Methods 17
array
array()
object
object()
is_null
is_null(self)
is_number
is_number(self)
is_bool
is_bool(self)
is_str
is_str(self)
is_array
is_array(self)
is_object
is_object(self)
as_bool
as_bool(self)
as_int
as_int(self)
as_float
as_float(self)
as_str
as_str(self)
append
append(self, value)
get
get(self, key: str, default = None)
values
values(self)
keys
keys(self)
items
items(self)
Special methods 13
__int__
__int__(self)
__float__
__float__(self)
__bool__
__bool__(self)
__len__
__len__(self)
__getitem__
__getitem__(self, index: int)
__getitem__(self, key: str)
__setitem__
__setitem__(self, index: int, value)
__setitem__(self, key: str, value)
__delitem__
__delitem__(self, index: int)
__delitem__(self, key: str)
__contains__
__contains__(self, key: str)
__str__
__str__(self)
__repr__
__repr__(self)
__eq__
__eq__(self, other: JSON)
__ne__
__ne__(self, other: JSON)
__hash__
__hash__(self)
class JSONDecodeError
class JSONDecodeError
Fields 5
msg str
doc str
pos int
lineno int
colno int
Constructors 1
__init__
__init__(self, msg: str, doc: str, pos: int)
class JSONDecoder
class JSONDecoder
Fields 2
strict bool
max_depth int
Constructors 1
__init__
__init__(self, strict: bool = True, max_depth: int = 512)
Methods 2
raw_decode
raw_decode(self, source: str, idx: int = 0)
decode
decode(self, source: str)
function loads
loads(source, strict: bool = True, max_depth: int = 512)
function load
load(fp, strict: bool = True, max_depth: int = 512)
class JSONEncoder
class JSONEncoder
Fields 8
ensure_ascii bool
allow_nan bool
check_circular bool
sort_keys bool
pretty bool
indent str
separators Tuple[str, str]
max_depth int
Constructors 1
__init__
__init__(self, ensure_ascii: bool = True, check_circular: bool = True, allow_nan: bool = True, indent = None, separators: Optional[Tuple[str, str]] = None, sort_keys: bool = False, max_depth: int = 512)
Methods 1
encode
encode(self, value)
function dumps
dumps(value, ensure_ascii: bool = True, check_circular: bool = True, allow_nan: bool = True, indent = None, separators: Optional[Tuple[str, str]] = None, sort_keys: bool = False, max_depth: int = 512)
function dump
dump(value, fp, ensure_ascii: bool = True, check_circular: bool = True, allow_nan: bool = True, indent = None, separators: Optional[Tuple[str, str]] = None, sort_keys: bool = False, max_depth: int = 512)