Ask a Question

How to generate multiple data from json response in data source

sumeetb
Occasional Contributor

How to generate multiple data from json response in data source

I have data source as test step and i am trying to extract multiple ids returned in json response. I am using this Row path "$.summary.test[0].id[0]", then it generate 1 row with correct data , but if i am removing '[0]' ($.summary.test.id) then its not generating any rows.

 

How do i generate all rows from Json response matching with id.?

9 REPLIES 9
TanyaYatskovska
SmartBear Alumni (Retired)

Hi sumeetb,

 

I suppose that you selected the ID node - that's why you receive the data only for one element. You need to select a parent node of IDs. According to your code, it should be either summary or test. Does it help?

 

---------
Tanya Yatskovskaya
SmartBear Community and Education Manager



nmrao
Community Hero

Show the raw response, please?


Regards,
Rao.
sumeetb
Occasional Contributor

Please see json response below.

 

{
"summary": [{
"id": "26590924327313990",
"name": "Summary-1",
"state": "ACTIVE",
"runTimestamp": 1473138585467,
"scheduledTimestamp": 1473138585507,
"createdTimestamp": 1473138585507,
"test": [{
"id": "15473842955298105",
"name": "test-1",
"createdTimestamp": 1473138585467,
"runTimestamp": 1473138585467,
"status": {
"Pass": 10,
"Fail": 20
}
}]
},
{
"id": "30493603067815445",
"name": "Summary-2",
"state": "ACTIVE",
"runTimestamp": 1473138254517,
"scheduledTimestamp": 1473138254520,
"createdTimestamp": 1473138254520,
"test": [{
"id": "6780837264054677",
"name": "Test -2 ",
"createdTimestamp": 1473138254517,
"runTimestamp": 1473138254517,
"status": {
"Pass": 1,
"Fail": 2
}
}]
}]
}

 

I need to extract all ids of summary.test.id 

i checked with summary.test still not able to get values. its returning zero rows.

nmrao
Community Hero

Here is the groovy script:

 

import groovy.json.JsonSlurper
​def response = '''{     "summary":[  
      {  
         "id":"26590924327313990",
         "name":"Summary-1",
         "state":"ACTIVE",
         "runTimestamp":1473138585467,
         "scheduledTimestamp":1473138585507,
         "createdTimestamp":1473138585507,
         "test":[  
            {  
               "id":"15473842955298105",
               "name":"test-1",
               "createdTimestamp":1473138585467,
               "runTimestamp":1473138585467,
               "status":{  
                  "Pass":10,
                  "Fail":20
               }
            }
         ]
      },
      {  
         "id":"30493603067815445",
         "name":"Summary-2",
         "state":"ACTIVE",
         "runTimestamp":1473138254517,
         "scheduledTimestamp":1473138254520,
         "createdTimestamp":1473138254520,
         "test":[  
            {  
               "id":"6780837264054677",
               "name":"Test -2 ",
               "createdTimestamp":1473138254517,
               "runTimestamp":1473138254517,
               "status":{  
                  "Pass":1,
                  "Fail":2
               }
            }
         ]
      }
   ]

}'''
def summary = new JsonSlurper().parseText(response).summary
def expectedIds = [15473842955298105, 6780837264054677]
def actualIds = []
summary.each { summaryItem ->
   summaryItem.test.each { testItem -> 
      actualIds << testItem.id 
   }
}
log.info actualIds
assert actualIds.sort() == expectedIds.sort(), "Test Ids are not matching"
​


Regards,
Rao.
nmrao
Community Hero

The problem is that test is not direct child of summary, it is part of array. and similarly id is also not direct child. So, you could not get them by using summary.test.id



Regards,
Rao.
sumeetb
Occasional Contributor

I wanted to use those extracted ids into another REST request by adding datasource loop. That's why wanted to generate those in data source and use them in another REST request. 

 

I am not sure how i can set data source rows with extracted ids using groovy script of data source. Could you please help?

nmrao
Community Hero

Can't you use data source of groovy type?



Regards,
Rao.
sumeetb
Occasional Contributor

I can but i am not able to set the property variable named as "id" with multiple rows. Please see below script.

 

import net.sf.*
import net.sf.json.*
import net.sf.json.groovy.*
def response = context.expand('${Get Summary#Response}' )
def jsonSlurper = new JsonSlurper().parseText(response)
for(String id:jsonSlurper.summary.test.id){

result["id"]=id

}

 

The result i get is property variable "id" is getting set with last value only (with this only 6780837264054677)

nmrao
Community Hero

Can you show test case structure?


Regards,
Rao.
cancel
Showing results for 
Search instead for 
Did you mean: