How do I extract just the msg value from a Pydantic ValidationError object?
I tried with:
json.loads(ValidationError_obj.json()[]['msg'])
But I am trying to find some simple built-in method.
How do I extract just the msg value from a Pydantic ValidationError object?
I tried with:
json.loads(ValidationError_obj.json()[]['msg'])
But I am trying to find some simple built-in method.
Copyright © 2021 Jogjafile Inc.
ValidationError.jsonreturns a string in JSON format. That is not what you want.The
ValidationError.errorsmethod returns a list of dictionaries, wherein each dictionary represents one of the errors accumulated in theValidationError.Example:
Output: (formatted for readability)
As you can see in this example, when there is more than one error, there will be more than one dictionary, each with its own
msgkey.If you are only interested in the first (or you know for a fact that there will be only one specific error), you can just do this:
Output:
value is not a valid integer