Comparing JSON array responses from 2 different get apis with mapper logic

I have Json arrays from 2 different APIs

For Example
1 https://test1.com/api
2 https://test2.com/api

Now below is array response that i have for both the APIs
1.
“resposne1”:[
{
“name”: “Speed1”,
“number”: “1017”,
“status”: “Active”
},
{
“name”: “Speed2”,
“number”: “1018”,
“status”: “Inactive”
}
]

2
“resposne2”:[
{
“t_name”:“Complete”,
t1:
{
“No”: “1017”,
“Test”: “Check”
},
“Flag”:“Active”
},
{
“t_name”:“Speed1”,
t1:
{
“No”: “1018”,
“Test”: “Check”
},
“Flag”:“Inactive”
}
]

Below is the mapper file that i am using

{
“name”: “t_name”,
“number”: “No”,
“status”: “Flag”
}

I need to compare the fields as mentioned in the mapper file for each array element in the json response and to compare both the responses i am savind the json response in a dictionary using Evaluate json.loads keyword but somehow it is giving an error while converting it to dictionary like below:

${json1_dict} = Evaluate json.loads(‘’‘${json1}’‘’, strict=False) json

so not able to move further and compare fields

There are 2 things for which i require a help.

1 How to use Evaluate json.loads to save json response in a dictionary , currently it is giving an error like [FAIL] Evaluating expression 'json.loads
2 How to use the sub element within json response like in the 2nd json response array field For Example, resposne2. t1.No to compare using the mapper file.

Thanks,
Rahul

Hi Rahul,

if you are using RequestsLibrary then you can make your life easier with${response.json()}, see Response Object, ${response.json()} will give you the json as a dictonary which makes it easy to get values out of it, but also means you should be able to use Dictionaries Should Be Equal from Collections Library.

Hope that helps,

Dave.

Hello Dave,

I have used that ${response.json()} also but the thing is it is taking too much time in execution.

Used below logic for the same

1st API has 21 array elements
2nd API has 25 array elements

so i have used nested for loops like from first API i=0 , iterated through j=0 to 24 and continue the same for i=1 to 20.

and also within the nested for loops used the nested if loops to compare each fields within the array elements so it is taking around 2 mins to execute and i need to have the execution time within 30 sec as there are other 500 json responses that i need to compare with each array in a single automation script.

so is there any other way within python/robot framework via which the time can be reduced using any other logic.

As mentioned over the original question i have tried the mapper logic but than it has below 2 issues.

1 How to use Evaluate json.loads to save json response in a dictionary , currently it is giving an error like [FAIL] Evaluating expression 'json.loads
2 How to use the sub element within json response like in the 2nd json response array field For Example, resposne2. t1.No to compare using the mapper file.

Thanks,
Rahul

Hi Rahul,

What’s taking the time is it Requests converting the JSON to a dictionary, or your loops iterating over the elements?

I would expect that Dictionaries Should Be Equal from Collections Library, that I mentioned before would be quicker, but as you mentioned 1st API has 21 array elements and 2nd API has 25 array elements, so you might have to use it on the first 21 array elements.

There is also List Should Contain Value, so as you iterate over the 21 array elements from the 1st API, you could use this to check the same dictionary item was in the list of 25 array elements from the 2nd API, this might be quicker than your nested loop?

Another idea you could try is when you iterate your i loop before the j loop use Convert To String to convert the i element to a string, then in the j loop do the same with each j element, then you can use Should Be Equal As Strings to compare the whole dictionary element in a single call rather than checking each sub element, but I think the List Should Contain Value idea will probably be quicker still.

Beyond that, are all the 500 json responses that you need to compare in separate test cases? does it really matter if they take 2 minutes if you can run them in parallel? This is where pabot would probably help. How many you can run in parallel will depend on your hardware and how many machines you can dedicate to the problem, but if you can run 64 in parallel then even taking 2 min each you’re still under 16 hours.

Dave.

Hello Dave,

Thank you for your response.

  1. What’s taking the time is it Requests converting the JSON to a dictionary, or your loops iterating over the elements? => Iterating over the elements is taking more time

  2. Another idea you could try is when you iterate your i loop before …
    =>Yes i have done the same but in that we need to use nested for loops and again nested if conditions because the scenario is like if we get primary element in both the responses matched than only going ahead to compare other elements of json array response. but in that if condition i have used like

IF ‘${array1.Element1}’==‘${array2.element1}’
Log To Console Primary Element is matched
IF ‘${array1.Element2}’==‘${array2.element2}’
Log To Console Second Element from both array is matched
END
END

but now i will try with [List Should Contain Value] approach.

  1. Beyond that, are all the 500 json responses that …
    => I need to compare the responses in 1 test case like each vendor is having 2 json responses and there are around 500 vendor having this kind of json responses so i need to either save the resource code in static json file within the project and extract that vendor code from the file and for each vendor compare the array element. will explore pabot as i am not much aware about it.

Again Thank you very much for spending time to understand the scenario i am trying to achieve.

Regards,
Rahul

1 Like