Forum Discussion

nsn's avatar
nsn
New Contributor
8 years ago

How to assert a JSON Array with 100+ values for its key names

Hi,

 

I am have to assert a JSON response using SOAPUI. This response has got an array of 300 elements. It looks something like this:

{
"apiVersion": "0.1",
"errors": null,
"infos": null,
"results": [{
"aReference": "12",
"aBlockItems": {
"aBlockItem": [{
"key": "key1",
"value": null
}, {
"key": "key2",
"value": "N"
}, {
"key": "key3",
"value": "N"
}, {
"key": "key4",
"value": "N"
}, {
"key": "key5",
"value": "0"
}, {
"key": "key6",
"value": "0"

 

and so on and so forth. I need to assert that all the keys - key1 till key300 are all present in the response. I am trying to use groovy script and couldn't find a way to do this.

 

Can someone help me with this?

Thanks

NS

4 Replies

  • nmrao's avatar
    nmrao
    Community Hero
    How do you want do it? What is the format of your expected data? a csv? a json? other format? Sample data would be helpful.
    • nsn's avatar
      nsn
      New Contributor

      hi, the snippet of JSON I pasted above is the response I get from API. I want to ensure that the keys are there in the response I am getting. Hope that makes sense.

  • avidCoder's avatar
    avidCoder
    Super Contributor

    Hey, You can write groovy code for JsonPath parser and verify the results. Follow this line of code:-

     

    This is your JSON:- 

     

    {
    "apiVersion": "0.1",
    "errors": null,
    "infos": null,

    }

     

    Get the path of each and every key whichever you wan to assert and then verify the results:-

    def response = '''

    {
    "apiVersion": "0.1",
    "errors": null,
    "infos": null

    }'''

    def slurper = new JsonSlurper();
    def inputdata= slurper.parseText(response)

     

    def finalValueToRead = "$.apiVersion"
    def actualValue = parse(inputdata).read(strFinalValueToRead).toString()

     

    actualValue will print 0.1 .. Similarly you do for others also. Better way is create a keyword names as "Check_Value_Present" and call it every time for the path verification.. That's all.

    • nsn's avatar
      nsn
      New Contributor

      Thanks avidCoder.