MCPcopy
hub / github.com/pydantic/pydantic / model_validate_json

Method model_validate_json

pydantic/main.py:743–784  ·  view source on GitHub ↗

!!! abstract "Usage Documentation" [JSON Parsing](../concepts/json.md#json-parsing) Validate the given JSON data against the Pydantic model. Args: json_data: The JSON data to validate. strict: Whether to enforce types strictly. extra:

(
        cls,
        json_data: str | bytes | bytearray,
        *,
        strict: bool | None = None,
        extra: ExtraValues | None = None,
        context: Any | None = None,
        by_alias: bool | None = None,
        by_name: bool | None = None,
    )

Source from the content-addressed store, hash-verified

741
742 @classmethod
743 def model_validate_json(
744 cls,
745 json_data: str | bytes | bytearray,
746 *,
747 strict: bool | None = None,
748 extra: ExtraValues | None = None,
749 context: Any | None = None,
750 by_alias: bool | None = None,
751 by_name: bool | None = None,
752 ) -> Self:
753 """!!! abstract "Usage Documentation"
754 [JSON Parsing](../concepts/json.md#json-parsing)
755
756 Validate the given JSON data against the Pydantic model.
757
758 Args:
759 json_data: The JSON data to validate.
760 strict: Whether to enforce types strictly.
761 extra: Whether to ignore, allow, or forbid extra data during model validation.
762 See the [`extra` configuration value][pydantic.ConfigDict.extra] for details.
763 context: Extra variables to pass to the validator.
764 by_alias: Whether to use the field's alias when validating against the provided input data.
765 by_name: Whether to use the field's name when validating against the provided input data.
766
767 Returns:
768 The validated Pydantic model.
769
770 Raises:
771 ValidationError: If `json_data` is not a JSON string or the object could not be validated.
772 """
773 # `__tracebackhide__` tells pytest and some other tools to omit this function from tracebacks
774 __tracebackhide__ = True
775
776 if by_alias is False and by_name is not True:
777 raise PydanticUserError(
778 'At least one of `by_alias` or `by_name` must be set to True.',
779 code='validate-by-alias-and-name-false',
780 )
781
782 return cls.__pydantic_validator__.validate_json(
783 json_data, strict=strict, extra=extra, context=context, by_alias=by_alias, by_name=by_name
784 )
785
786 @classmethod
787 def model_validate_strings(

Calls 2

PydanticUserErrorClass · 0.85
validate_jsonMethod · 0.45