Skip to content

Valid json

ValidJSON

Bases: BaseValidator

Class that provides functionality to check if a value matches a basic JSON structure.

Source code in dynamiq/nodes/validators/valid_json.py
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class ValidJSON(BaseValidator):
    """
    Class that provides functionality to check if a value matches a basic JSON structure.
    """

    def validate(self, content: str | dict):
        """
        Validates if the provided string is a properly formatted JSON.

        Args:
            content(str): The value to check.

        Raises:
            ValueError: If the value is not a properly formatted JSON.

        """
        try:
            if not isinstance(content, str):
                content = json.dumps(content)

            json.loads(content)
        except (json.decoder.JSONDecodeError, TypeError) as error:
            raise ValueError(
                f"Value is not valid JSON. Value: '{content}'. Error details: {str(error)}"
            )

validate(content)

Validates if the provided string is a properly formatted JSON.

Parameters:

Name Type Description Default
content(str)

The value to check.

required

Raises:

Type Description
ValueError

If the value is not a properly formatted JSON.

Source code in dynamiq/nodes/validators/valid_json.py
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
def validate(self, content: str | dict):
    """
    Validates if the provided string is a properly formatted JSON.

    Args:
        content(str): The value to check.

    Raises:
        ValueError: If the value is not a properly formatted JSON.

    """
    try:
        if not isinstance(content, str):
            content = json.dumps(content)

        json.loads(content)
    except (json.decoder.JSONDecodeError, TypeError) as error:
        raise ValueError(
            f"Value is not valid JSON. Value: '{content}'. Error details: {str(error)}"
        )