Ask a Question

JSON Request Parametrization

Regular Contributor

JSON Request Parametrization

I am looking how to do a json Request parameterization as I am holding a hundreds of JSON if I can't create every single json as a Test case/Rest request so I want to pass JSON from Excel File / Text file - Appreciate the help I i am

Community Hero


Are you using data-driven testing?
What is the skeleton of the test case / steps with their type?

Community Hero

If you have an excel or text file, I've found a datasource step using a file as the input worked best for me.


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

Yes I am using Data Driven testing 


JSON String is in Files  and Parameters are in Excel 


I need to Get JSON String from Files set parameters from Excel based on the json string and form a JSON Request dynamically 

Regular Contributor



appreciate if you share 

Community Hero

@678: I don't have a direct example to share. It looks like you have two files that work together, which may not work with a single File DataSource. If you can provide a sample of the txt file and excel file, I may be able to work up a set of DataSources and/or a groovy script.


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

Yes i am having a two files 


1) Json 2) Excel/csv 




Text File name :  Name.json

"userName": "${userName}",
"password": "${password}",



2) Excel / Csv Data 


userName password  inputjson

kumar       kumar123 Name.json

Frequent Contributor

Hello there! we have implemented very similar approach. i.e., reading a canned JSON from a file then replacing JSON objects from Data-source value.


Data-driven test case structure would look like this:



Groovy script would read JSON file and then replace the JSON objects and build new JSON payload for your REST request.



import net.sf.*
import net.sf.json.*
import net.sf.json.groovy.*
import groovy.json.JsonSlurper
import groovy.json.JsonBuilder

//## Get Directories ##//
def RequestPath = "C://TestUser//Request//" "Read canned JSON payload from $RequestPath" "Parsing JSON payload..."

//## READ canned request from a file to modify ##//
def JsonPayload = new File(RequestPath + "SampleJSON.txt").text
def JSONReq = new JsonSlurper().parseText JsonPayload

//## Get data from DataSource ##//
def userName = context.expand( '${DataSource#UserName}' )
def Password = context.expand( '${DataSource#Password}' ) "Updating json nodes..."

JSONReq.userName = "$userName"
JSONReq.password = "$Password" "Updating JSON Payload at testCase level property eSignJSON"

//## build json body and write to a file for reference ##//
def REQUEST = new JsonBuilder(JSONReq).toString()
    testRunner.testCase.setPropertyValue("JSONPayload", REQUEST) "JSON payload is $REQUEST"



In the Rest request point "JSONPayload" property to get the latest payload.

i..e., ${#TestCase#JSONPayload}


Hope this helps!


Regular Contributor




thank you but this is for only one json payload what if i have multiple JSON payload files ?

how can i loop one file read json payload update json nodes based on data source and execute request and come back loop again read json payload - update json nodes and execute ?

Frequent Contributor

Well, by looking at your example I assumed only Username and Password fields are referring to variables (or properties). File name looked constant. My bad.


I will have to think over if we could loop through files from a directory.  Another workaround would be having four fields in CSV or excel.



UserName  Password  FileName   File

TestUser1   Test123    JSON1.txt  C://TestUser//JsonFiles//JSON1.txt

TestUser2   Test345    JSON2.txt  C://TestUser//JsonFiles//JSON2.txt

TestUser3   Test678    JSON3.txt  C://TestUser//JsonFiles//JSON3.txt


Load this data into data-source and then use same groovy script to build json payload.



Showing results for 
Search instead for 
Did you mean: