jschon.json

class jschon.json.JSON(value, *, parent=None, key=None, itemclass=None, **itemkwargs)

An implementation of the JSON data model.

__bool__()

Return bool(self).

Return type

bool

__delitem__(index)

Delete self[index].

Supported for JSON types array and object.

Parameters

index (Union[int, str]) –

Return type

None

__eq__(other)

Return self == other.

Parameters

other (Union[jschon.json.JSON, None, bool, int, float, str, Sequence[Any], Mapping[str, Any]]) –

Return type

bool

__ge__(other)

Return self >= other.

Supported for JSON types number and string.

Parameters

other (Union[jschon.json.JSON, int, float, str]) –

Return type

bool

__getitem__(index)

Return self[index].

Supported for JSON types array and object.

Parameters

index (Union[int, slice, str]) –

Return type

jschon.json.JSON

__gt__(other)

Return self > other.

Supported for JSON types number and string.

Parameters

other (Union[jschon.json.JSON, int, float, str]) –

Return type

bool

__init__(value, *, parent=None, key=None, itemclass=None, **itemkwargs)

Initialize a JSON instance from the given JSON-compatible value.

The parent, key, itemclass and itemkwargs parameters should typically only be used in the construction of compound JSON documents by JSON subclasses.

Parameters
  • value (Union[None, bool, int, float, str, Sequence[Any], Mapping[str, Any]]) – a JSON-compatible Python object

  • parent (Optional[jschon.json.JSON]) – the parent node of the instance

  • key (Optional[str]) – the index of the instance within its parent

  • itemclass (Optional[Type[jschon.json.JSON]]) – the JSON subclass used to instantiate child nodes of arrays and objects (default: JSON)

  • itemkwargs (Any) – keyword arguments to pass to the itemclass constructor

__iter__()

Return iter(self).

Supported for JSON types array and object.

Return type

Iterator

__le__(other)

Return self <= other.

Supported for JSON types number and string.

Parameters

other (Union[jschon.json.JSON, int, float, str]) –

Return type

bool

__len__()

Return len(self).

Supported for JSON types string, array and object.

Return type

int

__lt__(other)

Return self < other.

Supported for JSON types number and string.

Parameters

other (Union[jschon.json.JSON, int, float, str]) –

Return type

bool

__repr__()

Return repr(self).

Return type

str

__setitem__(index, obj)

Set self[index] to obj.

Supported for JSON types array and object.

Parameters
  • index (Union[int, str]) –

  • obj (Union[jschon.json.JSON, None, bool, int, float, str, Sequence[Any], Mapping[str, Any]]) –

Return type

None

__str__()

Return str(self).

Return type

str

add(path, obj)

Add obj at path relative to self.

The JSON equivalent to add(), this method performs an in-place JSON Patch add operation on self.

If path is empty, the value of self is replaced by obj.

Parameters
Return type

None

copy(from_, to)
Parameters
Return type

None

dumpf(path)

Serialize the instance data to a JSON file.

Parameters

path (Union[str, os.PathLike]) – the path to the file

Return type

None

dumps()

Serialize the instance data to a JSON string.

Return type

str

insert(index, obj)

Insert obj before index.

Supported for JSON type array.

Parameters
  • index (int) –

  • obj (Union[jschon.json.JSON, None, bool, int, float, str, Sequence[Any], Mapping[str, Any]]) –

Return type

None

classmethod loadf(path, **kwargs)

Deserialize a JSON file to a JSON instance.

Parameters
  • path (Union[str, os.PathLike]) – the path to the file

  • kwargs (Any) – keyword arguments to pass to the JSON (subclass) constructor

Return type

jschon.json.JSON

classmethod loadr(url, **kwargs)

Deserialize a remote JSON resource to a JSON instance.

Parameters
  • url (str) – the URL of the resource

  • kwargs (Any) – keyword arguments to pass to the JSON (subclass) constructor

Return type

jschon.json.JSON

classmethod loads(value, **kwargs)

Deserialize a JSON string to a JSON instance.

Parameters
  • value (str) – the JSON string

  • kwargs (Any) – keyword arguments to pass to the JSON (subclass) constructor

Return type

jschon.json.JSON

move(from_, to)
Parameters
Return type

None

remove(path)

Remove the instance at path relative to self.

The JSON equivalent to remove(), this method performs an in-place JSON Patch remove operation on self.

If path is empty, the value of self is set to None.

Parameters

path (Union[str, jschon.jsonpointer.JSONPointer]) –

Return type

None

replace(path, obj)

Set obj at path relative to self.

The JSON equivalent to replace(), this method performs an in-place JSON Patch replace operation on self.

If path is empty, the value of self is replaced by obj.

Parameters
Return type

None

test(path, obj)
Parameters
Return type

None

data: Union[None, bool, int, float, str, List[jschon.json.JSON], Dict[str, jschon.json.JSON]]

The instance data.

JSON type

data type

null

None

boolean

bool

number

int | float

string

str

array

list[JSON]

object

dict[str, JSON]

itemclass: Type[jschon.json.JSON]

The JSON class type of child instances.

itemkwargs: Dict[str, Any]

Keyword arguments to the itemclass constructor.

key: Optional[str]

The index of the instance within its parent.

parent: Optional[jschon.json.JSON]

The containing JSON instance.

property path: jschon.jsonpointer.JSONPointer

Return the path to the instance from the document root.

type: str

The JSON type of the instance. One of null, boolean, number, string, array, object.

property value: Union[None, bool, int, float, str, Sequence[Any], Mapping[str, Any]]

Return the instance data as a JSON-compatible Python object.

jschon.json.JSONCompatible

Type hint for a JSON-compatible Python object.

alias of Union[None, bool, int, float, str, Sequence[Any], Mapping[str, Any]]

jschon.json.false = False

Use to represent the JSON false value literally in Python code.

jschon.json.null = None

Use to represent the JSON null value literally in Python code.

jschon.json.true = True

Use to represent the JSON true value literally in Python code.