Ask a Question

how to compare JSON response with JDBC request, when JDBC request contains CDATA string

Gkm
Contributor

how to compare JSON response with JDBC request, when JDBC request contains CDATA string

 

import com.eviware.soapui.support.XmlHolder
import groovy.json.JsonSlurper

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}" '''

// replace "(starting&ending), [](starting&ending) and \ with space from response
def TrimResponse =JSONResponse.replaceAll('^\"|\"$','').replaceAll('\\\\','').replaceAll('\"\\[','')replaceAll('\\]\"','')
def json = new JsonSlurper().parseText(TrimResponse)

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>12</UNITID>
        </Row>
    </ResultSet>
</Results> '''

def holder = new XmlHolder(response)

//Get ConfigurationDetailXML Node value using Xpath from holder object, which will retrive CDATA
def configurationXmlCdata =  holder.getNodeValue('//*:CONFIGURATIONDETAILXML')

def cdataHolder = new XmlHolder(configurationXmlCdata)

I'm able to compare each node like this

 

def id = json.Unit.Devices.Id.toString().replaceAll('\\[','').replaceAll('\\]','')
def jsonpip = json.Unit.Devices.PIPDetailJSON.toString().replaceAll('\\[','').replaceAll('\\]','')

def dbid = cdataHolder.getNodeValue('//ROOT/VideoDevice/Id')
def pip = cdataHolder.getNodeValue('//ROOT/VideoDevice/PIPDevices/Devices')
def dbpip = new JsonSlurper().parseText(pip).toString().replaceAll('\\[','').replaceAll('\\]','')

def error = []
if (id != dbid) {error.add("id: $id is not equal to dbid: $dbid ")}
if (jsonpip != dbpip) {error.add("jsonpip: $jsonpip is not equal to dbpip: $dbpip ")}

assert error.size() == 0," $error " 
if(error.size() == 0){ log.info "Script Passed" } 

But it is not a good way as I have so many values to compare 😞

 

18 REPLIES 18
nmrao
Community Hero

Re: how to compare JSON response with JDBC request, when JDBC request contains CDATA string

Is the actual problem with comparison or showing differences if any?


Regards,
Rao.
Gkm
Contributor

Re: how to compare JSON response with JDBC request, when JDBC request contains CDATA string

First compare and if any difference then print the difference, but I don't want to do this node by node .

nmrao
Community Hero

Re: how to compare JSON response with JDBC request, when JDBC request contains CDATA string

Can't be done thru looping?


Regards,
Rao.
Gkm
Contributor

Re: how to compare JSON response with JDBC request, when JDBC request contains CDATA string

I don't know how to do this by loop

nmrao
Community Hero

Re: how to compare JSON response with JDBC request, when JDBC request contains CDATA string

Gkm
Contributor

Re: how to compare JSON response with JDBC request, when JDBC request contains CDATA string

Great thanks let me try first πŸ™‚

btw can do same fashion with this JSON response ?

nmrao
Community Hero

Re: how to compare JSON response with JDBC request, when JDBC request contains CDATA string

Gkm
Contributor

Re: how to compare JSON response with JDBC request, when JDBC request contains CDATA string

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
nmrao
Community Hero

Re: how to compare JSON response with JDBC request, when JDBC request contains CDATA string

Can you post raw json response please? Why are you getting slashes?


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