To validate JSON against a JSON Schema in Robot Framework, the most common and robust approach is using robotframework-jsonlibrary. It provides native keywords to validate JSON objects directly or by reading a schema file.
Using robotframework-jsonlibrary (Recommended)
The JSONLibrary provides dedicated, high-level keywords to validate JSON strings or files directly against a schema.
Installation
pip install robotframework-jsonlibrary
Example :
*** Settings *** Library JSONLibrary Library OperatingSystem *** Test Cases *** Validate JSON Against Schema File # Load your JSON response or target data ${json_data}= Load Json From File ${CURDIR}/response.json # Validate it directly against the schema file Validate Json By Schema File ${json_data} ${CURDIR}/schema.json Validate JSON Inline String ${json_string}= Set Variable {"id": 1, "name": "John Doe"} ${schema_string}= Set Variable {"type": "object", "properties": {"id": {"type": "integer"}, "name": {"type": "string"}}, "required": ["id", "name"]} ${json_object}= Convert String To Json ${json_string} Validate Json By Schema ${json_object} ${schema_string}

No comments:
Post a Comment