Molecule JSONizer

class MoleculeJsonizer(key_makers=(InchiKey(),))[source]

Bases: object

Abstract base class for creating JSONs of molecules.


You might notice that the public methods of this abstract base class are implemented. These are just default implementations, which can be safely ignored or overridden, when implementing subclasses. However, the default implementation can be used directly, if it suits your needs.


Converting a Molecule to JSON

You want to create a JSON representation of a molecule

import stk

jsonizer = stk.MoleculeJsonizer()
json = jsonizer.to_json(stk.BuildingBlock('NCCN'))

Adding Additional Molecular Keys

Apart from atoms, bonds and the position matrix, the JSON representation holds additional fields, one for each MoleculeKeyMaker provided to the initializer

import stk

jsonizer = stk.MoleculeJsonizer(
json = jsonizer.to_json(stk.BuildingBlock('NCCN'))

In this case, json will look something like

    # A tuple of JSON atom representations.
    'atoms': (...),

    # A tuple of JSON bond representations.
    'bonds': (...),

    'InChI': 'The InChI of the molecule',
    'InChIKey': 'The InChIKey of the molecule',

For every MoleculeKeyMaker provided to key_makers, a new key will be added to the JSON representation, with its name given by MoleculeKeyMaker.get_key_name() and the value given by MoleculeKeyMaker.get_key().



Return a JSON representation of molecule.


Initialize a MoleculeJsonizer instance.


key_makers (Union[MoleculeKeyMaker, Iterable[MoleculeKeyMaker]]) – Used to make the keys of molecules, which should be included in their JSON representations. Keys allow molecular data to reference itself when split across multiple JSONs.


Return a JSON representation of molecule.


molecule (Molecule) – The molecule to serialize.

Return type



A JSON representation of molecule.