cancel
Showing results for 
Search instead for 
Did you mean: 

compare a response with a json file

SOLVED
Highlighted
Contributor

compare a response with a json file

Hey,

 

first of all i have a GET request with a response... now i wanna save the response in a JSON file to compare this with a existing JSON file.

 

HOW can i do this?

 

best regards

kai

1 ACCEPTED SOLUTION

Accepted Solutions
Community Hero

Re: save a response in a json file

You can use the following code:

 

...
if (!(it.value == outputResp[it.key]))
    {
        log.info ("File Mismatch at : " + it.key)
        testRunner.fail( "File mismatch..." )
    }
...


If my answer helped please click on the 'Accept as Solution' button.
6 REPLIES 6
Community Hero

Re: save a response in a json file

This should be simple to do. But just to clarify before giving an answer:

 

1. If you want to compare the response with an existing content of a file why do you want to save the response to a file. Wouldn't it be simpler to just read the content of the initial file and compare it directly with the current response.

2. I am not aware of the full context of your test but I generally see this kind of tests unreliable. If something is to change in the response you may have to do a lot of rework.



If my answer helped please click on the 'Accept as Solution' button.
Contributor

Re: save a response in a json file

1.Yes. Is that possible?

2. True!

 

i found some Code and it works but maybe u can help me. If there is a Mismatch there is only a log.info and the test continues with "PASS". Is taht possible to say is there a mismatch the test failed?

 

import groovy.json.JsonSlurper
def filePath = "C:/qwerty/test.json"
def jsonResp = context.expand('${GET ProCon#Response}') 

def outputResp = new JsonSlurper().parseText(jsonResp)
def baselineResp = new JsonSlurper().parseText(new File(filePath).text)

baselineResp.each{

    if (!(it.value == outputResp[it.key]))
    {
        log.info ("File Mismatch at : " + it.key)
    }

}
Frequent Contributor

Re: save a response in a json file

You can use a keyword assert i.e assert expected == actual 

Community Hero

Re: save a response in a json file

You can use the following code:

 

...
if (!(it.value == outputResp[it.key]))
    {
        log.info ("File Mismatch at : " + it.key)
        testRunner.fail( "File mismatch..." )
    }
...


If my answer helped please click on the 'Accept as Solution' button.
Contributor

Re: save a response in a json file

@Lucian
it works.
and now my next question. i have a [] in my json. Is there a error/mismatch in the [] let me take a example... lets say i have Items[] and in Itmes i have a OID = 123456, Name = XYZ for now the test can say me there is a error/mismatch in ITEMS but not where. It cant say error or mismatch in Items>OID.


@jhanzeb1

im complity new to groovy where i need to add "assert i.e assert expected == actual" and did i need to delete anything?

Contributor

Re: save a response in a json file

assuming you hit the if condition you mentioned you should just be able to use

assert (it.value == outputResp[it.key])

if it passes, it wouldn't have hit your if so nothing would have been logged out, but if it fails it will hit the if and subsequently fail this assertion

 

Hope this helps,

Mo

If my answer helped you out, please consider leaving kudos and/or accepting as a solution.
Have an awesome day!
New Here?
Join us and watch the welcome video:
SeptemberNews
Join the exciting event
SeptemberHubBub
Top Kudoed Authors
Join the September Hub-bub to show off, learn and win