0

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

4
  • I have imported the JSONLibrary and then tried load json from file , it gave me the error No keyword with the name load json from file. Commented Sep 29, 2021 at 9:12
  • I am new to the JSON schema approach actually, if it was for a single response i could have used should be equal keyboard Commented Sep 29, 2021 at 9:14
  • This answer shows how to load JSON: stackoverflow.com/questions/24734629/… Commented Sep 29, 2021 at 13:05
  • @Pekka I have added the code in the question please have a look into it Commented Sep 29, 2021 at 13:30

1 Answer 1

2

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}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.