clarksj71
13 years agoOccasional Contributor
JSON integer validation
I'm using this groovy script to slurp the json response from a test case and save it to the project properties, but when I come accross an integer my script fails. I'm at the end of my rope an need some help...I believe the response is a string, but I don't know how to convert it....please show me how 
Groovy Script
import net.sf.json.groovy.*
def i = testRunner.testCase.getTestStepByName("Read - Single Valid Case Number").testRequest.response.responseContent
def jsonResponse = i
def response = new JsonSlurper().parseText(jsonResponse)
testRunner.testCase.testSuite.project.setPropertyValue("swId", response.swId )
testRunner.testCase.testSuite.project.setPropertyValue("svOrigin", response.svOrigin )
Example REST/JSON response
{
"Case": {
"swId": 217907,
"swReportedBy": "369074.00000",
"svOrigHelpDesk": "HJP-3",
"svOrigin": "Automation",
"swType": "Inquiry",
"swSubject": "Services - Test3",
"brPriority": "4 - Low",
"svXrefTicketNum": null,
"svcId": 515328,
"swStatus": "Open - Customer Hold",
"svRFO": "Customer treat.",
"escalatedToFirstName": null,
"escalatedToLastName": null,
"reportedByFirstName": "San",
"reportedByLastName": "Pan",
"swSiteName": "Media LTD",
"brLogicalName": "S217907",
"swAddress1": "123 Main",
"swCity": "Delhi",
"swZip": "110001",
"swCustomerId": 168648,
"brSiteInstProdId": null,
"svEscDeptId": null,
"svResponsible": "Customer",
"swInstProdId": null,
"svDateClosed": "Sep 15 2005 12:06AM",
"svDateModified": "Apr 8 2013 4:00PM",
"swCreatedBy": "suzuki",
"swDateCreated": "Aug 22 2005 1:50AM",
"svAcctInstProdId": null
},
"originatingSystem": "hello"
}

Groovy Script
import net.sf.json.groovy.*
def i = testRunner.testCase.getTestStepByName("Read - Single Valid Case Number").testRequest.response.responseContent
def jsonResponse = i
def response = new JsonSlurper().parseText(jsonResponse)
testRunner.testCase.testSuite.project.setPropertyValue("swId", response.swId )
testRunner.testCase.testSuite.project.setPropertyValue("svOrigin", response.svOrigin )
Example REST/JSON response
{
"Case": {
"swId": 217907,
"swReportedBy": "369074.00000",
"svOrigHelpDesk": "HJP-3",
"svOrigin": "Automation",
"swType": "Inquiry",
"swSubject": "Services - Test3",
"brPriority": "4 - Low",
"svXrefTicketNum": null,
"svcId": 515328,
"swStatus": "Open - Customer Hold",
"svRFO": "Customer treat.",
"escalatedToFirstName": null,
"escalatedToLastName": null,
"reportedByFirstName": "San",
"reportedByLastName": "Pan",
"swSiteName": "Media LTD",
"brLogicalName": "S217907",
"swAddress1": "123 Main",
"swCity": "Delhi",
"swZip": "110001",
"swCustomerId": 168648,
"brSiteInstProdId": null,
"svEscDeptId": null,
"svResponsible": "Customer",
"swInstProdId": null,
"svDateClosed": "Sep 15 2005 12:06AM",
"svDateModified": "Apr 8 2013 4:00PM",
"swCreatedBy": "suzuki",
"swDateCreated": "Aug 22 2005 1:50AM",
"svAcctInstProdId": null
},
"originatingSystem": "hello"
}