Forum Discussion
https://github.com/nmrao/soapUIGroovyScripts/blob/master/groovy/AssertJsonArrayElement.groovy
I have to add separate array for CDATA string , here is the code I'm able to get all JDBC values.
import com.eviware.soapui.support.XmlHolder import groovy.json.* import groovy.xml.* def response = ''' <Results> <ResultSet fetchSize="128"> <Row rowNumber="1"> <CONFIGURATIONDETAILXML> <![CDATA[ <ROOT> <Profile>12</Profile> <BitRate>3072</BitRate> <VideoDevice> <Id>1</Id> <Devices>[{"Name":"ALPR_1","Profile":{"ID":1,"BitRate":0},"Index":-1}]</Devices> <PIPDevices> <Devices>[{"Name":"ALPR_2","Profile":{"ID":2,"BitRate":0},"Mode":"pip"}]</Devices> </PIPDevices> </VideoDevice> </ROOT> ]]> </CONFIGURATIONDETAILXML> <UNITID>Gita</UNITID> </Row> </ResultSet> </Results> ''' def holder = new XmlHolder(response) def configurationXmlCdata = holder.getNodeValue('//*:CONFIGURATIONDETAILXML') def XmlCdata = new XmlSlurper().parseText(configurationXmlCdata) def Cdatajdbc = [] XmlCdata.each { row -> Cdatajdbc.addAll(row.Profile,row.VideoDevice) log.info Cdatajdbc } def jdbcResponseSlurper = new XmlSlurper().parseText(response) def jdbc = [] jdbcResponseSlurper.ResultSet.Row.each { row -> jdbc.addAll(row.UNITID) log.info jdbc }
But I'm getting error in case of JSON ,
//def JSONResponse = context.expand('${unitconfig#Response}') def JSONResponse = ''' "{\"Unit\":{\"Profile\":12,\"Devices\":[{\"Id\":1,\"DetailJSON\":\"[{\\\"Name\\\":\\\"ALPR_1\\\",\\\"Profile\\\":{\\\"ID\\\":1,\\\"BitRate\\\":0},\\\"Index\\\":-1}]\",\"PIPDetailJSON\":[\"[{\\\"Name\\\":\\\"ALPR_2\\\",\\\"Profile\\\":{\\\"ID\\\":2,\\\"BitRate\\\":0,\\\"Mode\\\":\\\"pip\\\"}]\"]}],\"UnitID\":12}" ''' def TrimResponse =JSONResponse.replaceAll('^\"|\"$','').replaceAll('\\\\','').replaceAll('\"\\[','')replaceAll('\\]\"','') def jsonResponseSlurper = new JsonSlurper().parseText(TrimResponse) log.info jsonResponseSlurper.Unit.Profile // 12 is displaying def json = [] jsonResponseSlurper.Unit.each { r -> json.addAll(r.Profile) // error displays log.info json }
- ERROR:groovy.lang.MissingPropertyException: No such property: Profile for class: java.util.HashMap$Entry
- nmrao8 years agoChampion Level 3Can you post raw json response please? Why are you getting slashes?
- Gkm8 years agoContributor
This is actual JSON string response, and after removing all extra delemeters, I got this JSON
def JSONResponse = ''' { "Unit": { "Profile ": 12, "Devices ": [{ "Id ": 1, "DetailJSON": { "Name": "ALPR_1", "Profile": { "ID": 1, "BitRate": 0 }, "Index": -1 }, "PIPDetailJSON": { "Name": "ALPR_2", "Profile": { "ID": 2, "BitRate": 0, "Mode": "pip" } } }] }, "UnitID ": 12 } '''
- nmrao8 years agoChampion Level 3I am not sure the json response you attached is full or partial. It is difficult to tell Unless full json response available. I believe that you are the best just judge, which elements to loop.
- Gkm8 years agoContributor
This is just an example , my original json response has 304 values :smileyvery-happy:
I want to assert all the elements of response
- Gkm8 years agoContributor
nmrao, I have multiple values under Unit as provided Profile and UnitID, I got your point able to fetch all values of Unit.devices but unable to fetch values which are directly child of Unit like Profile and UnitID. I have so many values under Unit so yes I want to loop them as well.
- nmrao8 years agoChampion Level 3Ok, so basically, your object is not that simple, putting all the values into one class. You may need to have hierarchy of objects in the model class to be able to compare.
- Gkm8 years agoContributor
Difficult to manage class , so I just break whole complex thing in sections, loop node and compare , Unit section is remaining in which I have profile , I don't know why it is returning null.
log.info jsonResponseSlurper.Unit.Profile
- nmrao8 years agoChampion Level 3
Based on the sample json, here is I would model the classes (hierarchial approach to create complex data)
Sample Json
{ "Unit": { "Profile": 12, "Devices": [ { "Id": 1, "DetailJSON": { "Name": "ALPR_1", "Profile": { "ID": 1, "BitRate": 0 }, "Index": -1 }, "PIPDetailJSON": { "Name": "ALPR_2", "Profile": { "ID": 2, "BitRate": 0, "Mode": "pip" } } } ] }, "UnitID ": 12 }
Object Modeling
class Profile { def id def bitRate def mode } abstract class Details { def name Profile profile } class PipDetailJson extends Details {} class DetailJson extends Details { def index } class Device { def id DetailJson dJson PipDetailJson pJson } class Unit { def profile List<Device> devices }
/**top level object **/ class Model { Unit unit def unitId }