Ask a Question

How to Compare an array from one Json Response to an array from another Json response.

SOLVED
dipsthorat
New Contributor

How to Compare an array from one Json Response to an array from another Json response.

Hi All,

I have 2 different json array and need to validate every n each attribute against each other .Earlier I used Message content assertion but I need to do for all array item manually. for example there is array for "Route"

is there any other way to compare array of response ?

dipsthorat_0-1615330123156.png

dipsthorat_1-1615330245546.png

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
nmrao
Community Hero

Re: How to Compare an array from one Json Response to an array from another Json response.

Here is a sample to compare two lists (books in this case). But you need to adopt the changes as per your data.

 

 

def expectedJson = '''{
    "book": [
        {
            "title": "Beginning JSON",
            "price": 49.99
        },

        {
            "title": "JSON at Work",
            "price": 29.99
        }
    ]
}'''

def actualJson = '''{
    "book": [
        {
            "title": "JSON at Work",
            "price": 29.99
        },
        {
            "title": "Beginning JSON",
            "price": 49.99
        }
    ]
}'''

//Note that the order of books are different in the above 

def getJson = { str -> new groovy.json.JsonSlurper().parseText(str)}
def expectedBooks = getJson(expectedJson).book
def actualBooks = getJson(actualJson).book

//Check if both lists are sorted and compared
assert actualBooks.sort() == expectedBooks.sort()

 



Regards,
Rao.

View solution in original post

4 REPLIES 4
Fisher021
Occasional Visitor

Re: How to Compare an array from one Json Response to an array from another Json response.

Extract the data from the response body (JSON object array) in the form of JSON string Glasshouse view of code quality with every check-in In automating the requests, we need to pass the data in many requests in different forms.

 

expressHR

nmrao
Community Hero

Re: How to Compare an array from one Json Response to an array from another Json response.

You need to use groovy to achieve the same.


Regards,
Rao.
dipsthorat
New Contributor

Re: How to Compare an array from one Json Response to an array from another Json response.

Can you please help on this to me @nmrao
nmrao
Community Hero

Re: How to Compare an array from one Json Response to an array from another Json response.

Here is a sample to compare two lists (books in this case). But you need to adopt the changes as per your data.

 

 

def expectedJson = '''{
    "book": [
        {
            "title": "Beginning JSON",
            "price": 49.99
        },

        {
            "title": "JSON at Work",
            "price": 29.99
        }
    ]
}'''

def actualJson = '''{
    "book": [
        {
            "title": "JSON at Work",
            "price": 29.99
        },
        {
            "title": "Beginning JSON",
            "price": 49.99
        }
    ]
}'''

//Note that the order of books are different in the above 

def getJson = { str -> new groovy.json.JsonSlurper().parseText(str)}
def expectedBooks = getJson(expectedJson).book
def actualBooks = getJson(actualJson).book

//Check if both lists are sorted and compared
assert actualBooks.sort() == expectedBooks.sort()

 



Regards,
Rao.

View solution in original post

cancel
Showing results for 
Search instead for 
Did you mean: