Forum Discussion

sukanya's avatar
sukanya
Contributor
7 years ago
Solved

How to extract data from URL in json node

I just want to extract navTok value in the Next link actually.

for example :

"NEXT" :"https://localhost:8080/navTok=72638726387sjhgfdsjfgsj82364"

The thing is i need to extract the navTok value using groovy script.

 

Please do the needful on this.

 

  • Hi sukanya

     

    The code which i provided above will do the same which you want to do

     

    Copy and paste it in script assertion

     

    And it will save you Dara in testcase level property

10 Replies

  • groovyguy's avatar
    groovyguy
    Community Hero

    Do you have a groovy script you are working with already? If so, we can likely build off of that. 

    • sukanya's avatar
      sukanya
      Contributor

      I actually want to extract the Next value and store it in a value named "NextLink"  and also i need to extract the navTok value from the below Json response

  • Hi sukanya

     

    Here you go, just look at below code snippet and modify it accordingly if you need any:

     

    //This is for extracting Next Link data
    //imports
    import groovy.json.JsonSlurper
    
    //grab the response
    def ResponseMessage = messageExchange.response.responseContent
    
    //define a JsonSlurper
    def jsonSlurper = new JsonSlurper().parseText(ResponseMessage)
    
    //verify the slurper isn't empty
    assert !(jsonSlurper.isEmpty())
    
    def NextData = jsonSlurper.Links.Next
    //it will store the value of Next in TestCase level properties
    context.testCase.setPropertyValue("NextData",NextData)
    
    
    //This is For navTok Data
    
    NextData = NextData.toString()
    strLen = NextData.length()
    
    if(NextData.contains("navTok"))
    {	
    	navtokIndex = NextData.lastIndexOf("navTok")
    	navtokIndex = navtokIndex + 7
    	navtokString = NextData.substring(navtokIndex, strLen)
    	context.testCase.setPropertyValue("navTokData",navtokString)	
    }
    else
    {
           assert(true:false):"String not found"
    }

     

    Do mark it as a solution if it fulfills your need. :smileyvery-happy:

     

    Cheers,

    Himanshu

     

    • sukanya's avatar
      sukanya
      Contributor

      Thanks for your solution. I am trying it to work out. Will post it as soon as I am done.

    • sukanya's avatar
      sukanya
      Contributor
      //grab the response
      def ResponseMessage = messageExchange.response.responseContent

      Thank for your inputs. 

      1. After running the RESTRequst, got the response in SoapUI.

      2. Would like to grab the above response(Point# 1) and assign it to the variable "ResponseMessage". Please refer the attached screen shots and advise on this.

       

       

      • HimanshuTayal's avatar
        HimanshuTayal
        Community Hero
        //grab the response
        def ResponseMessage = messageExchange.response.responseContent

         

        With this code you saved your response in variable named ResponseMessage.

        I am not getting where are you stucking?

        or what you exactly wanna do?

         

        Thanks,

        Himanshu 

  • Hi sukanya

     

    The code which i provided above will do the same which you want to do

     

    Copy and paste it in script assertion

     

    And it will save you Dara in testcase level property

  • Hi,

     

    Yes, it worked. And also i just need code to do pagination. I want the next links to be clicked and get the other responses. 

    Can you please help me in doing that.