Ask a Question

How to assert on value in a Json response?

mgroen2
Super Contributor

How to assert on value in a Json response?

Hi,

what would be the code to assert the value of "accesstoken" = " 1234"  in Soap UI?

 

Which of the available Assertions do I need to pick (I guess XPath match, but not sure)?

 

and what would be the Regex?

2017-04-03_14-56-31.png

 

Thanks for the help.

 

8 REPLIES 8
nmrao
Community Hero

Re: How to assert on value in a Json response?

For the mentioned case, JsonPatMatch is fine.

$.accessToken should equal to the desired value.


Regards,
Rao.
nmrao
Community Hero

Re: How to assert on value in a Json response?

If you want to retrieve the token, and use it for further requests, then Script Assertion can be used.

You may find some sample scripts below:
https://github.com/nmrao/soapUIGroovyScripts/tree/master/groovy


Regards,
Rao.
mgroen2
Super Contributor

Re: How to assert on value in a Json response?


@nmrao wrote:
For the mentioned case, JsonPatMatch is fine.

$.accessToken should equal to the desired value.

@nmraoI entered this (see screenshot), but it's not working. as you can see there is an accesstoken in the response, but validating it gives an error (it thinks it should be false?). Do you have any tips?

2017-04-03_17-00-30.png

 

nmrao
Community Hero

Re: How to assert on value in a Json response?

You should be able to try here
http://jsonpath.com/

Following Script Assertion should help:

def parsedJson = new groovy.json.JsonSlurper().parseText(context.response)
log.info parsedJson.accessToken
//replace expectedValue below
assert expectedValue == parsedJson.accessToken, 'Not matched.'

You can also check here:
http://groovyconsole.appspot.com/script/5179916383420416


Regards,
Rao.
HKosova
Moderator

Re: How to assert on value in a Json response?

@mgroen2, you're using JsonPath Regex Match. Try the regular JsonPath Match instead.


Helen Kosova
SmartBear Documentation Team Lead
________________________
Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️
mgroen2
Super Contributor

Re: How to assert on value in a Json response?

Hi,

 

@HKosova as you suggested, I implemented the JsonPath Match, 

the strange this is, that validation initially succeeds (I clicked on "select from current"), however when re-executing the test it fails, and the reason for that is that expected value is changed, a comma is added to the end (see second screenshot).

 

After re-selecting the value again (" select from current"), the assertion passes again first time, but re-executing the test, makes the assertion fail again (comma is added again). Is this known behaviour? Possibly a bug?

 

2017-04-04_15-10-06.png

2017-04-04_15-04-31.png

 

 

mgroen2
Super Contributor

Re: How to assert on value in a Json response?

@nmrao thanks for the link.

 

HKosova
Moderator

Re: How to assert on value in a Json response?

Mathijs,

 

The comma is part of the error message, not the value. And the error happens because the token value is different - e.g. on your image the expected value ends with "...D8LE" and the actual value ends with "... JSPPA" (or vice versa).

 

I guess the token is generated dynamically and is different for each new session, like a unique session ID. How are you supposed to validate it? Do you just need to verify this token is present in the response, regardless of the value?


Helen Kosova
SmartBear Documentation Team Lead
________________________
Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️
cancel
Showing results for 
Search instead for 
Did you mean: