I am trying to validate two API JSON response key-value pairs for same key values and throw an error if there is a mismatch in the JSON response.
Eg: 1. www.oldurl.com gives=>
{ "firstName":"alex", "age":31, "dob":"10-12-1988" }
2. www.newurl.com gives =>
{ "firstName":"**alx**", "**ag**":31, "dob":"10-12-1988" }
Here the oldurl and newurl gives the same JSON response but in newurl we see an error in key and values.
Now, I need to catch this error and show the user that there is a mismatch in newurl.com with key firstname and age.
Python code:
import unittest
import requests, json,settings
from requests.auth import HTTPBasicAuth
from jsonschema import validate
class TestOne(unittest.TestCase):
def setUp(self):
def test_oldurl(self):
resp=requests.get('www.oldurl.com',auth=HTTPBasicAuth(settings.USER_NAME,settings.PASSWORD))
data = resp.text
print (data) #Prints json
def test_newurl(self):
resp=requests.get('www.newurl.com',auth=HTTPBasicAuth(settings.USER_NAME,settings.PASSWORD))
data = resp.text
print (data) #Prints json
now I got two JSON response, How can I validate these two responses. Is there any python libraries that can validate and show any error in keys and values.
note: Both JSON response should be same I am doing this as part of validation to avoid any error in the future response.
I also used schema for one JSON responses key validation alone. used:
def setUp(self):
with open('Blueprint_schema.json', 'r') as f:
self.schema = f.read()
self.file = f
validate(json.loads(data), json.loads(self.schema))
self.assertNotEqual(data,'[]')
but this is helping only for one JSON response key alone. So, I need to compare two API's JSON response while executing or by storing it in two files and opening it and validating. I thought these files validation but it will be more coding so thought of reducing the length of code by validating on runtime itself.
Please suggest your ideas.