Groovy web console

subscribe to the feed Subscribe
to this
site
jsonQuery (via #groovywebconsole)
tweet this snippet Tweet
this
script

jsonQuery

Published 2 months ago by nmrao with tags compare json
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
def jsonRequest = """{  
   "deptId":"4",
   "branchId":"6",
   "firstName":"Prabhu",
   "lastName":"S",
   "emailAddress": "prabhus@gmail.com",
   "contactNumber":"98765432",
   "isEnabled":"Y",
   "dob":"02/05/1994",
   "doj":"03/04/2016",
   "fatherName":"XXXX",
   "motherName":"YYYY",
   "activationCode":"ssp",
   "spouseName":"ZZZZ",
   "address":"MMMMMM",
   "pincode":"123456",
   "district":"16",
   "status":"0"
}"""

def jsonResponse = """{  
   "empid":"4187",
   "deptId":"4",
   "branchId":"6",
   "firstName":"Prabhu",
   "lastName":"S",
   "emailAddress":"prabhus@gmail.com",
   "contactNumber":"987654321",
   "isEnabled":"Y",
   "dob":"02/05/1994",
   "doj":"03/04/2016",
   "fatherName":"XXXX",
   "motherName":"YYYY",
   "activationCode":"ssp",
   "spouseName":"ZZZZ",
   "address":"MMMMMM",
   "pincode":"12345",
   "district":"16",
   "dateCreated":"2017-02-21T13:00:24.317Z",
   "dateModified":"2017-02-21T13:00:24.317Z",
   "status":"0"
}"""

def reqParsed = new groovy.json.JsonSlurper().parseText(jsonRequest)
def resParsed = new groovy.json.JsonSlurper().parseText(jsonResponse)
def ignoreKeys = ['empid', 'dateCreated', 'dateModified']
def sb = new StringBuffer()
reqParsed.keySet().each { key ->   
   if (!ignoreKeys.contains(key)) {
      reqParsed."$key" == resParsed."$key" ?: sb.append("${key} value does not match; request[${reqParsed."$key"}], response[${resParsed."$key"}]\n")

   }
}
if(sb.toString()) {
   throw new Error("There are differences between the request values and response values. Details below:\n${sb.toString()}")
}