Ask a Question

How to extract data from URL in json node

SOLVED
sukanya
Contributor

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.

 

10 REPLIES 10
groovyguy
Community Hero

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




---

Click the Accept as Solution button if my answer has helped, and remember to give kudos where appropriate too!

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

HimanshuTayal
Community Hero

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. Smiley Very Happy

 

Cheers,

Himanshu

 


Click "Accept as Solution" if my answer has helped,
Remember to give "Kudos" 🙂 ↓↓↓↓↓



Thanks and Regards,
Himanshu Tayal

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

//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.

 

 

//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 


Click "Accept as Solution" if my answer has helped,
Remember to give "Kudos" 🙂 ↓↓↓↓↓



Thanks and Regards,
Himanshu Tayal

I am actually new to groovy. I just want to know how we can save the response In a variable.

Below mentioned is my json response. I just want to get the Next link and store it in a NextLink named property. also i need to get navTok value for all the next urls in the response. Can you please let me know what are all the properties I need to add at property transfer step and also at data source step.

 

HimanshuTayal
Community Hero

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


Click "Accept as Solution" if my answer has helped,
Remember to give "Kudos" 🙂 ↓↓↓↓↓



Thanks and Regards,
Himanshu Tayal
cancel
Showing results for 
Search instead for 
Did you mean: