Reputation: 33
I have a JSON response which returns all the details of the users in a company. I have converted those responses to JSON schema and kept it in a file name JSONSchema.json.
Now how do I compare all the responses with the JSON schema ? Can someone please help.
JSON Data
[ {
"Id": 2,
"Name": "Test123",
"Status": "Active"
},
{
"Id": 3,
"Name": "123Test",
"Status": "Active"
}
]
JSON Schema
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "array",
"items": [
{
"type": "object",
"properties": {
"Id": {
"type": "integer"
},
"Name": {
"type": "string"
},
"Status": {
"type": "string"
}
},
"required": [
"Id",
"Name",
"Status"
]
},
{
"type": "object",
"properties": {
"Id": {
"type": "integer"
},
"Name": {
"type": "string"
},
"Status": {
"type": "string"
}
},
"required": [
"Id",
"Name",
"Status"
]
}
]
}
Code
*** Settings ***
Library jsonschema
*** Test Cases ***
${get_value}= GET On Session xyz /details
${json_response}= set variable ${get_value.json()}
${schema} Get Binary File ./myschema.json
${schema} evaluate json.loads('''${schema}''') json
${instance} evaluate json.loads('''{"Name":[0,1]}''') json
validate instance=${instance} schema=${schema}
I have issues with the last two lines of the code.
I want the my json response compare to the schema as give a pass message when I get compare the Name : Test123 with the type string in JSON schema
Upvotes: 0
Views: 3703
Reputation: 2280
I am not super familiar with requests library but you could use validate a bit like this:
*** Settings ***
Library RequestsLibrary
Library jsonschema
Suite Setup Init payloads
*** Test Cases ***
Schematest
${response}= Post url=http://postman-echo.com/post json=${payload}
${response_json}= Set Variable ${response.json()['data']}
Log To Console ${response_json}
Evaluate jsonschema.validate(instance=${response_json}, schema=${schema})
*** Keywords ***
Init payloads
${payload_str}= Catenate SEPARATOR=
... [ {
... "Id": 2,
... "Name": "Test123",
... "Status": "Active"
... },
... {
... "Id": 3,
... "Name": "123Test",
... "Status": "Active"
... }
... ]
${payload}= Evaluate json.loads('''${payload_str}''') json
Set Suite Variable ${payload}
${schema_str}= Catenate SEPARATOR=
... {
... "$schema": "http://json-schema.org/draft-04/schema#",
... "type": "array",
... "items": [
... {
... "type": "object",
... "properties": {
... "Id": {
... "type": "integer"
... },
... "Name": {
... "type": "string"
... },
... "Status": {
... "type": "string"
... }
... },
... "required": [
... "Id",
... "Name",
... "Status"
... ]
... },
... {
... "type": "object",
... "properties": {
... "Id": {
... "type": "integer"
... },
... "Name": {
... "type": "string"
... },
... "Status": {
... "type": "string"
... }
... },
... "required": [
... "Id",
... "Name",
... "Status"
... ]
... }
... ]
... }
${schema}= Evaluate json.loads('''${schema_str}''') json
Set Suite Variable ${schema}
Upvotes: 2