rest - Incorrect properties displayed from Json -
i having issue when trying collect data json using soap ui , groovy scripting. below example json:
{ "regions": [{ "regionid": 10, "hotels": [{ "roominformation": [{ "hotelroomid": 1, "availablerooms": 1, "roomprice": 100, "occupancysequenceorder": 1, "providerroomid": 1, "isextranet": true }], "regionid": 1, "hotelid": 1, "boardtypeid": 1, "startdate": "2017-10-23t00:00:00", "enddate": "2017-10-30t00:00:00", "totalprice": 1000, "providerinformation": { "hotelspecialofferdetails": [], "rateplancode": "xxx", "allavailableresults": [] }, "providerhotelid": 25 }, { "roominformation": [{ "hotelroomid": 1, "availablerooms": 1, "roomprice": 100, "occupancysequenceorder": 1, "providerroomid": 1, "isextranet": true }], "regionid": 1, "hotelid": 1, "boardtypeid": 1, "startdate": "2017-10-23t00:00:00", "enddate": "2017-10-30t00:00:00", "totalprice": 1000, "providerinformation": { "hotelspecialofferdetails": [], "rateplancode": "ggg", "allavailableresults": [] }, "providerhotelid": 31 } ], "errors": null }], "errors": null }
what want select first instance of providerhotelid
, rateplancode
. have groovy script below tackle this:
def alert = com.eviware.soapui.support.uisupport import groovy.json.jsonslurper def response = testrunner.testcase.getteststepbyname("search region").getproperty("response").getvalue(); def jsonres = new jsonslurper().parsetext(response); def providerhotelid = jsonres.regions.hotels.providerhotelid[0].tostring() def rateplancode = jsonres.regions.hotels.providerinformation[0].rateplancode.tostring() log.info providerhotelid testrunner.testcase.setpropertyvalue('providerhotelid', providerhotelid) testrunner.testcase.setpropertyvalue('rateplancode', rateplancode)
this outputs below in custom properties:
- providerhotelid - [25,31]
- rateplancode - [xxx]
the above incorrect because:
- providerhotelid - displays provider hotel ids when want first 1 should
25
. - rateplancode - correct displays [] around , want removed. same goes providerhotelid.
so example custom properties should display:
- providerhotelid - 25
- rateplancode - xxx
how can achieved within groovy script?
here need:
//get values, falatten them , first 1 def providerhotelid = jsonres.regions.hotels.providerhotelid.flatten()[0] def rateplancode = jsonres.regions.hotels.providerinformation.rateplancode.flatten()[0] log.info providerhotelid log.info rateplancode
you can try online demo
Comments
Post a Comment