Forum Discussion

m_atty's avatar
m_atty
Occasional Contributor
7 years ago

How to test a JSON response with multiple objects with the same name

Hi,

 

I'm looking for some ideas on how we can test one of our new API's. The API in question will return a JSON response similar to the one outlined below:

 

 

{
   "testJSON" : [
      {
         "test1" : "111",
         "test2" : "ABC"
      },
      {
         "test1" : "222",
         "test2" : "DEF"
      }
   ]
}

As the JSON response can have multiple objects with the same name (e.g. we will have multiple objects called test1), we've been having difficulties finding a way to create assertions to allow us to check that the each object contains the correct value without manually creating properties for each object.

 

Ideally, we want to be able to use an Excel spreadsheet which will show each JSON section on a separate row (e.g. the columns will be titled test1 and test2 and each row will display the expected content for each section of the JSON). We'd then want an assertion test step to compare the JSON response to this spreadsheet.

 

We've tried a few methods of doing this in SoapUI (we've tried data sources, assertion test steps, data sinks etc) but they all start to fail as soon as we have more than one object with the same name.

 

My guess is we'll need to write a groovy script to do this, but I just wondered if anyone had any experience with JSON's in this format and how they managed to get around the above issues?

 

Please let me know if you want me to add any more info into the question.

 

Thanks in advance for your help.

Matt

 

 

 

  • richie's avatar
    richie
    Community Hero

    Hi Matt,

     

    Jsonpath will identify the different instances of the test1 attribute - so is it a case that there could be different number of test1 instances returned for different scenarios - or will the same number of test1 instances be returned whatever is submitted in the request?

     

    there have been several posts recently about json responses returning multiple instances of an object, name attribute, array or whatever when the number of instances change depending on the scenario submitted - I'm wondering if these other posts might lead you to the correct solution?

     

    cheers,

     

    richie

    • m_atty's avatar
      m_atty
      Occasional Contributor

      Hi Richie,

       

      We could have different number of test1 instances returned depending on the scenario. For example, we could request to see all of our customers in Lincolnshire which could return 100 instances of test1 - whereas requesting to see all of our customer in Scotland may return 300 instances. Hope that makes a bit more sense.

       

      I've had a look through the other posts that seemed to be linked to this request, but none seemed to answer this specific query. I'll have another scan through them now to check the most recent responses.

       

      Thanks,

      Matt