Forum Discussion

rajshk04's avatar
rajshk04
New Contributor
7 years ago

split json

success_Response (java.lang.Object)

{"TTLRN":{"address":"tel:+9999999999","validity":"Valued","carrierid":"0000","carriername":"MYDORA","numbertype":"L","country":"Uganda","portingstatus":"false","mcc":"641","mnc":"11","deactivationdetail":{"carrierid":"","carriername":"","deactivationdate":""},"deactivationdate":"","requestid":"nistesting9","trackingid":"101580039301503395442144"}}

def success_Response = context.expand( '${TTLRN Request#Response#$}' )

def stringOutputSplitResponse=[]


stringOutputSplitResponse[] = success_Response.split(/"/)

def string = stringOutputSplitResponse[1]

log.info string

 

 

am unable to print TTLRN, expecting string to have assigned the value TTLRN

7 Replies

  • groovyguy's avatar
    groovyguy
    Champion Level 1

    Maybe this will help.

     

    package json
    import com.eviware.soapui.support.XmlHolder
    import groovy.json.*
    
    // get the response of the JSON test step / request. 
    // def response = context.expand( '${TestStepName s#Response}' ).toString()
    // below is a string representing the value provided in the forum post. The above line can replace the below, with appropriate name of JSON response location
    
    def success_Response = context.expand( '${TTLRN Request#Response#$}' )
    
    def r = new JsonSlurper().parseText(success_Response)
    
    log.info(r["TTLRN"])
    
    • rajshk04's avatar
      rajshk04
      New Contributor

      package json
      import com.eviware.soapui.support.XmlHolder
      import groovy.json.*

      // get the response of the JSON test step / request.
      // def response = context.expand( '${TestStepName s#Response}' ).toString()
      // below is a string representing the value provided in the forum post.
      //The above line can replace the below, with appropriate name of JSON response location

      def response = context.expand( '${NOS 2.3 Request#Response}' ).toString()


      def r = new JsonSlurper().parseText(response)
      log.info(r["TTLRN"])


      getting the following

       

      response (java.lang.Object){"TTLRN":{"address":"tel:+9999999999","validity":"yes","carrierid":"ABCDE","carriername":"Best Mobile","numbertype":"M","country":"Angola","deactivationdetail":{"carrierid":"","carriername":"","deactivationdate":""},"deactivationdate":"","requestid":"test123","trackingid":"101"}}
      r (java.lang.Object) 

       

      There is no response for log.info(r["TTLRN"])

      • groovyguy's avatar
        groovyguy
        Champion Level 1

        I don't think you want to use this:

         

        def response = context.expand( '${NOS 2.3 Request#Response}' ).toString()

        Try this?

         

        def response = context.expand( '${NOS 2.3 Request#Response}' )
  • nmrao's avatar
    nmrao
    Champion Level 3

    Would you mind providing raw response? and what data you need to extract?

  • nmrao's avatar
    nmrao
    Champion Level 3
    I think the question is unclear. What data out of json do you want?
    • rajshk04's avatar
      rajshk04
      New Contributor

      package json
      import com.eviware.soapui.support.XmlHolder
      import groovy.json.*

       

      def response = context.expand( '${Request#Response#$}' )
      def r = new JsonSlurper().parseText (response)

       

      //I am trying to parse the following json response, I am expecting r to store the following

      //{"TTLRN":{"address":"tel:+919886977381","validity":"true","carrierid":"ABCD","carriername":"Myphone","type":"M","country":"India","portingstatus":"false","mcc":"eee","mnc":"000","mdetail":{"carrierid":"","carriername":"","rdate":""},"ddate":"","requestid":"test123","trackingid":"8002"}}

       

      log.info (r["TTLRN"])

       

      //not able to understand what it should print.

       

       

       

       

       

      • groovyguy's avatar
        groovyguy
        Champion Level 1

        Based on the code I gave you, r is storing:

        //{"TTLRN":{"address":"tel:+919886977381","validity":"true","carrierid":"ABCD","carriername":"Myphone","type":"M","country":"India","portingstatus":"false","mcc":"eee","mnc":"000","mdetail":{"carrierid":"","carriername":"","rdate":""},"ddate":"","requestid":"test123","trackingid":"8002"}}

         

        and

        log.info (r["TTLRN"])

         

        Will print out the contents of TTLRN:

        {"address":"tel:+919886977381","validity":"true","carrierid":"ABCD","carriername":"Myphone","type":"M","country":"India","portingstatus":"false","mcc":"eee","mnc":"000","mdetail":{"carrierid":"","carriername":"","rdate":""},"ddate":"","requestid":"test123","trackingid":"8002"}

         

        What do you want it to do? As nmrao has stated, you are vague on your requirements so we can only guess at what you need help with.