17.1.1.3.2.2. cobra.io.json

Provide functions for I/O in JSON format.

17.1.1.3.2.2.1. Module Contents

17.1.1.3.2.2.1.1. Functions

to_json(model: Model, sort: bool = False, **kwargs: Any) → str

Return the model as a JSON string.

from_json(document: str) → ‘Model’

Load a cobra model from a JSON string.

save_json_model(model: Model, filename: Union[str, Path], sort: bool = False, pretty: bool = False, **kwargs: Any) → None

Write the cobra model to a file in JSON format.

load_json_model(filename: Union[str, Path]) → ‘Model’

Load a cobra model from a file in JSON format.

cobra.io.json.JSON_SPEC = 1[source]
cobra.io.json.to_json(model: Model, sort: bool = False, **kwargs: Any) → str[source]

Return the model as a JSON string.

Parameters
  • model (cobra.Model) – The cobra model to represent.

  • sort (bool, optional) – Whether to sort the metabolites, reactions, and genes or maintain the order defined in the model (default False).

  • **kwargs (Any) – Keyword arguments passed on to json.dumps.

Returns

JSON string representation of the cobra model.

Return type

str

See also

save_json_model()

Write directly to a file.

json.dumps()

Base function.

cobra.io.json.from_json(document: str) → 'Model'[source]

Load a cobra model from a JSON string.

Parameters

document (str) – The JSON string representation of a cobra model.

Returns

The cobra model as interpreted from the JSON string.

Return type

cobra.Model

See also

load_json_model()

Load directly from a file.

cobra.io.json.save_json_model(model: Model, filename: Union[str, Path], sort: bool = False, pretty: bool = False, **kwargs: Any) → None[source]

Write the cobra model to a file in JSON format.

Parameters
  • model (cobra.Model) – The cobra model to represent.

  • filename (str or file-like) – File path or descriptor that the JSON representation should be written to.

  • sort (bool, optional) – Whether to sort the metabolites, reactions, and genes or maintain the order defined in the model (default False).

  • pretty (bool, optional) – Whether to format the JSON more compactly (default) or in a more verbose but easier to read fashion. Can be partially overwritten by the **kwargs (default False).

  • **kwargs (Any) – Keyword arguments passed to json.dump.

See also

to_json()

Return a string representation.

json.dump()

Base function.

cobra.io.json.load_json_model(filename: Union[str, Path]) → 'Model'[source]

Load a cobra model from a file in JSON format.

Parameters

filename (str or file-like) – File path or descriptor that contains the JSON document describing the cobra model.

Returns

The cobra model as represented in the JSON document.

Return type

cobra.Model

See also

from_json()

Load from a JSON string.