Ask a Question

Re: Compare JSON & JDBC response using groovy

New Contributor

Re: Compare JSON & JDBC response using groovy


I am having the similar issue, but script returns empty result for the model object. I am not sure if I my class model is defined correctly. I am trying to compare some json response values with jdbc response values. 


import groovy.xml.XmlUtil
import groovy.util.XmlSlurper
import groovy.json.JsonSlurper
def groovyUtils = new

* Model object for comparing
class Model {
def id
def createdTime
def updatedTime

* this will accept the jdbc row
* @Param row
* @return
Model buildJdbcData(row) {
row.with {
createdTime = row.CREATED_TIMESTAMP
updatedTime = row.UPDATED_TIMESTAMP

* this will accept the json TagInfo
* @Param tagInfo
* @return
def buildJsonData(tagInfo){
id = tagInfo.eligibilityId
createdTime = tagInfo.eligibilityRequestCreated
updatedTime = tagInfo.eligibilityRequestUpdated

* def jdbcResponse = context.expand('${JdbcStepName#Response}')
def jdbcResponse = context.expand( '${JDBC Request1#ResponseAsXML#//Results/ResultSet/Row}')

// jdbcResponse

* If you want, you can assign the response received directly using below instead of current and make sure you replace the step name correctly
* def jsonResponse = context.expand('${JsonStepName#Response}')
def jsonResponse = context.expand( '${LocationList 1 - Request 1#ResponseAsXML}')
// jsonResponse

//Parsing the jdbc and build the jdbc model object list
def results = new XmlSlurper().parseText(jdbcResponse)
def jdbcObjects = []
def jdbcDataObjects = []
results.ResultSet.Row.each { Row->
jdbcDataObjects.add(new Model().buildJdbcData(Row))
} "JDBC Response -> "+ results "JDBC "+ jdbcDataObjects

//Parsing the json and build the json model object list
def arrayOfTagInfo = new XmlSlurper().parseText(context.responseAsXml)

//def arrayOfTagInfo = new JsonSlurper().parseText(jsonResponse)
def jsonDataObjects = []
arrayOfTagInfo.TagInfo.each { tagInfo ->
jsonDataObjects.add(new Model().buildJsonData(tagInfo))
} arrayOfTagInfo "JSON "+jsonDataObjects

//sorting the Data before checking for equality

if (jdbcDataObjects.size() != jsonDataObjects.size()) {
System.err.println("Jdbc resultset size is : ${jdbcDataObjects.size()} and Json result size is : ${jsonDataObjects.size()}")
assert jdbcDataObjects == jsonDataObjects, "Comparison of Jdbc and Json data is failed"




Community Hero

You asked a question relating to this thread - it has been moved to its own thread to help you get an answer.

Frequent Contributor

.. and can you edit your post to use the code tag </>  on the button bar.

Showing results for 
Search instead for 
Did you mean: