Forum Discussion

Nmenon81's avatar
Nmenon81
Occasional Contributor
6 years ago
Solved

JSON (REST) to JDBC resultset comparison

I want to compare all the elements of a JSON REST response to corresponding elements in the JDBC resultset. I have a working solution when I need to compare specific tag values but now I have a large...
  • Alex99's avatar
    6 years ago

    Something like this?

     

    new XmlSlurper().parseText(<JDBC_TEXT>).ResultSet.Row.children().each { it->
    	def key	= it.name().toLowerCase().replace("_", "")
    	def value	= it.text()
    	def foundElements = []
    
    	for(s in <JSON_TEXT>.findAll( /"[\w]+" : "?[\w.-]{0,}"?/ )) {
    		jsonkey   = s.replace("\"", "").split(":")[0].trim()
    		jsonvalue = s.replace("\"", "").split(":")[1].trim()
    
    		if(key.equalsIgnoreCase(jsonkey)) {
    			foundElements.add(s)
    		}
    	}
    
    	if(foundElements.size > 0) {
    		log.info "for [key=${key}] ${foundElements.size} elements were found ${foundElements}"	
    	} else {
    		log.error "for [key=${key}] no elements were found"
    	}
    }
    
    
    
    
    log.info "done"

    Result will look like this:

    ...
    Wed Oct 24 21:50:20 CEST 2018:INFO:for [key=uvwaiverfee] 1 elements were found ["UvWaiverFee" : ""]
    Wed Oct 24 21:50:20 CEST 2018:INFO:for [key=uvwaivercalc] 1 elements were found ["UvWaiverCalc" : ""]
    Wed Oct 24 21:50:20 CEST 2018:INFO:for [key=uvfillerlock] 1 elements were found ["UvFillerLock" : ""]
    Wed Oct 24 21:50:20 CEST 2018:ERROR:for [key=pbenupdcount] no elements were found
    Wed Oct 24 21:50:20 CEST 2018:ERROR:for [key=pbenkey0] no elements were found
    Wed Oct 24 21:50:20 CEST 2018:ERROR:for [key=rowcolumn] no elements were found
    Wed Oct 24 21:50:20 CEST 2018:INFO:done