Skip to content

json_artifact

Json = Union[dict[str, 'Json'], list['Json'], str, int, float, bool, None] module-attribute

JsonArtifact

Bases: BaseArtifact

Stores JSON data.

Attributes:

Name Type Description
value Json

The JSON data. Values will automatically be converted to a JSON-compatible format.

Source code in griptape/artifacts/json_artifact.py
@define
class JsonArtifact(BaseArtifact):
    """Stores JSON data.

    Attributes:
        value: The JSON data. Values will automatically be converted to a JSON-compatible format.
    """

    value: Json = field(converter=lambda value: JsonArtifact.value_to_json(value), metadata={"serializable": True})

    @classmethod
    def value_to_json(cls, value: Any) -> Json:
        if isinstance(value, str):
            return json.loads(value)
        else:
            return json.loads(json.dumps(value))

    def to_text(self) -> str:
        return json.dumps(self.value)

value: Json = field(converter=lambda value: JsonArtifact.value_to_json(value), metadata={'serializable': True}) class-attribute instance-attribute

to_text()

Source code in griptape/artifacts/json_artifact.py
def to_text(self) -> str:
    return json.dumps(self.value)

value_to_json(value) classmethod

Source code in griptape/artifacts/json_artifact.py
@classmethod
def value_to_json(cls, value: Any) -> Json:
    if isinstance(value, str):
        return json.loads(value)
    else:
        return json.loads(json.dumps(value))