Forum Discussion

hemantloya's avatar
hemantloya
New Contributor
6 years ago

How to pass NULL, BLANK and remove a field from JOSN using ready API

Hi ,   Please help on creating post request for below three scenarios using ready API.   Suppose I have JSON object with below definition. { "A" : "String", "B" : "String" } Field A  and ...
  • hemantloya's avatar
    hemantloya
    6 years ago

    Hi richie ,

     

    I tried below appoach to pass null, Blank("") and value for a field to json payload. 

     

    1) created datasource step ( getData), which has property bookName

    2) created a groovy which will do below.

    def strBookName = context.expand( '${getData#bookName}' )
    if(!(strBookName == "null"))
    {
          testRunner.testCase.testSteps["excelToJson"].setPropertyValue("bookName","\""+ strBookName + "\"")
    }
    else{
          testRunner.testCase.testSteps["excelToJson"].setPropertyValue("bookName", "null" )
    }
    3) excelToJson is properties step which has property bookName

    4) now my Post request payload looks like : 

    {
    "job": ${excelToJson#id},
    "name": ${excelToJson#bookName}
    }

     

    So when we have null value in excel cell, the else part of groovy will be executed and property bookName in step excelToJson will have null as value. When we have actual value than if part of the groovy will be executed and that actual value will be prefixed and postfixed by double quotes and same will be stored in bookName property of excelToJson.

     

    Thus we can achive "", null, "Jhon" all this scenarios.

     

    But really thanks for all the responses.

     

    Thanks

    Hemant