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

 

 

10 REPLIES 10
Fisher021
Occasional Visitor

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

You need to use groovy to achieve the same.


Regards,
Rao.

Can you please help on this to me @nmrao

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.

I Have really enjoyed this blog, you’re a great author.

myestub

Such an amazing story please share more stuff I really like these posts.

twitchbitstousd

FaxCoverSheet1
New Member

I have seen the first-time Article it is so wonderful thanks for sharing this article.

FaxCoverSheet

I have seen this article this is very nice thanks for sharing it with us.

fmcdealer

Thank you so much for this Article this is very useful.

myaccessflorida

cancel
Showing results for 
Search instead for 
Did you mean: