Forum Discussion

baechu123's avatar
baechu123
Occasional Contributor
8 years ago

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.

  • nmrao's avatar
    nmrao
    8 years ago

    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')
    }

     

  • nmrao's avatar
    nmrao
    Champion Level 3
    Yes, possible.

    Please explain your use case.
    How do you get the above json? And how are you going to use it?
    • baechu123's avatar
      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"
      }

      • nmrao's avatar
        nmrao
        Champion Level 3
        Are you ok if the entire file content is set as request to RESt Request test step?