Ask a Question

json block as a property value

SOLVED
baechu123
Occasional Contributor

json block as a property value

Is it possible to have a json block as my property value?

 

{
"firstName": "John",
"lastName" : "doe",
"age" : 26,
"address" : {
"streetAddress": "naist street",
"city" : "Nara",
"postalCode" : "630-0192"
}

 

I tried it by making it a single line and it worked. However, when it comes to large json data, it will be very hard to maintain it.

8 REPLIES 8
nmrao
Community Hero

Yes, possible.

Please explain your use case.
How do you get the above json? And how are you going to use it?


Regards,
Rao.
baechu123
Occasional Contributor

I have this txt file that contains my json data. This serves as my data source.

 

See attached file for example json data.

 

I have a request that requires that json block as its required body parameter.

 

In the txt file I have to use the whole address block as a property value so that I can pass it in my request.

 

Property name = address

Property value = {
"streetAddress": "naist street",
"city" : "Nara",
"postalCode" : "630-0192"
}

Are you ok if the entire file content is set as request to RESt Request test step?


Regards,
Rao.
baechu123
Occasional Contributor

Yes! Will that be possible? I've been trying to do that approach but I can't figure it out.

Define a test case level custom property say JSON_REQUEST_FILE and provide the file path. This is just not to have any hard-code stuff in the script. If it is property, then user can just change path there and avoid modifying the script.

 

Use forward slash ('/') as path separator even on windows.

 

Add a Groovy Script Test Step before your existing REST Request test step.

 

Below script would set the file content to next test step. Throw error oterwise.

 

def fileName = context.expand('${#TestCase#JSON_REQUEST_FILE}')
def fileContent = fileName ? new File(fileName)?.text : ''
if (fileContent ) {	context.testCase.testStepList[context.currentStepIndex+1].httpRequest.requestContent = fileContent
} else {
   throw new Error('Could not find property JSON_REQUEST_FILE or file, check it')
}

 



Regards,
Rao.
baechu123
Occasional Contributor

I'm lost here: "Define a test case level custom property say JSON_REQUEST_FILE and provide the file path. This is just not to have any hard-code stuff in the script. If it is property, then user can just change path there and avoid modifying the script."

 

Should I add a new REST Request and input the file path as its URL?

baechu123
Occasional Contributor

Ok I got it. The downside here is that if I have many request, I have to save my json data in a different file and different test case. Right?

 

Thanks!

In case, you want to set the file name from with in script, then it is well and good. Just assign the value in the below statement

def fileName = '/path/to/json'


Regards,
Rao.
cancel
Showing results for 
Search instead for 
Did you mean: