Forum Discussion

NK12's avatar
NK12
Occasional Contributor
7 years ago

How to get JDBC Response into an Array

Hello All,

I'm trying to compare JSON Response and DB Response

 

Below is the sample Code Snippet

import groovy.xml.*

import groovy.json.*

//DB Response
def dbresponse = context.expand( '${JDBC Request#ResponseAsXml}' )

log.info dbresponse

def dbsluper=new XmlSlurper().parseText(dbresponse)

log.info dbsluper

//Json Response

def jsonresponse = context.expand( '${REST Request#Response}' )

//log.info jsonresponse

def jsluper=new JsonSlurper().parseText(jsonresponse)

//log.info jsluper


//Writing the json specfic data into an array

def jsonvalues=[]
jsluper.STMT_BK_ADDR_LINE.each{
 jsonvalues.add(it.value)
}

jsonvalues.sort()

log.info jsonvalues

def dbvalues=[]
dbsluper.Results.each{
 dbvalues.add(it.value)
}
log.info dbvalues

 

Here I'm getting empty result set for dbvalues

 

Attached is the sample screen snippet

3 Replies

  • PaulMS's avatar
    PaulMS
    Super Contributor

    Try without Results because that is the root node and use text()

    dbsluper.ResultSet.each{
    dbvalues.add(it.text())
    }

    but that won't be an array because there is only 1 ResultSet.

     

    If you need the row/s children

    dbsluper.ResultSet.Row.children().each{
    dbvalues.add(it.text())
    }

     

  • nmrao's avatar
    nmrao
    Champion Level 3
    What are you trying to achieve? Would you mind elaborating the use case?
  • NK12's avatar
    NK12
    Occasional Contributor

    Thanks it worked out and able to get the result set in Array

     

    Here is the script

    import groovy.xml.*
    import groovy.json.*


    //DB Response
    def dbresponse = context.expand( '${JDBC Request#ResponseAsXml}' )

    log.info dbresponse

    def dbsluper=new XmlSlurper().parseText(dbresponse)

    log.info dbsluper


    //Json Response

    def jsonresponse = context.expand( '${REST Request#Response}' )

    log.info jsonresponse

    def jsluper=new JsonSlurper().parseText(jsonresponse)

    log.info jsluper


    //Writing the json specfic data into an array

    def jsonvalues=[]
    jsluper.DelinquencyRecord.each{
     jsonvalues.add(it.key)
     //jsonvalues.add(it.value)
    }

    jsonvalues.sort()
    log.info jsonvalues

    //Writing the DBData into array
    def dbvalues= []
    dbsluper.ResultSet.each{
     dbvalues.add(it.text())
    }

    log.info dbvalues

     

    1.How can I get Json data as (Key:Value) in my array(Here Json Array name is jsonvalues)

    so that I can compare the result set of DB and Json data

     

    nmrao

     

    Here I'm trying to compare the JSON response to DB Response