Data validation checks a value against stated requirements, such as an allowed data type, range or relationship between fields. Parsing establishes how input fits a syntax; an application's validation rules determine whether the resulting value is suitable for its purpose. In Python,
json.loads performs JSON deserialization, but it does not know a particular application's requirements.[ref]For an original example, suppose a program accepts an exact Python integer object from 0 through 10. Its validation predicate is
type(x) is int and 0 <= x <= 10. After JSON decoding, inputs 0, 3 and 10 pass; -1, 11, "3", true and 3.0 fail. All are valid JSON texts. The last input becomes a floating-point object, so this particular rule rejects it even though its numerical value is integral. A different application could deliberately accept it.The rule must therefore be explicit about its intended distinction. Converting every input to an integer before checking would change what is accepted and could lose information. Validating after such conversion answers a different question from checking the original decoded data type.
New to topics? Read the docs here!