Hi hemantloya
you can do this - but it's a bit fiddly - there are some problems in the way that I'll explain first - but before I have to ask - are you sure you want to do this? The reason I ask is that there are arguments about whether
"B" : "" is actually equivalent to "B" : null
do your system's requirements actually replace strings with a null rather than a blank (e.g. "")? for numerics and boolean types it makes sense (if empty attributes are supported) to replace the values with null - but for strings you dont typically replace "B" : "value" with "B" : null because of the fact you have to remove the double quotes before your json parser will recognise the null as null - hence the reason a lot of systems accept "B" : "" as empty for string types rather than "B":null
Anyway - the reasons being doing what you want is fiddly are as follows:
There are 4 principal datatypes in json
String/text (also date values are considered strings in json) - json requires double quote marks wrapping the value
Numeric - json doesn't allow double quotes to wrap the value
Boolean - json doesn't allow double quotes to wrap the value
Null - json doesn't allow double quotes to wrap the value
json wellformed rules follow the above pattern
{
"String/text type" : "string/text value",
"Numeric type" : 12345678,
"Boolean type":true/false,
"Null type" : null
}
// only the String/text type attributes allow double quotes to wrap the values
// "null" is not allowed - the value is null (without quotes)
json wellformed rules require a comma after each value
When you tried the following:
testRunner.testCase.testSteps["excelToJson"].setPropertyValue("job",null)
log.info testRunner.testCase.testSteps["excelToJson"].getPropertyValue("job")
Essentially nothing/no value (not even null) was written to the excelToJson's Property step's job property value - and therein lies your problem - I appreciate the logging will report null in the info response - but actually nothing is written in the job property's value field
Hence the reason when you use the following parameter as the value of 'B' attribute in your json
{"B":"${excelToJson#job}", "A":"String"}
you are generating
{"B":"", "A":"String"}
instead of what you want - which is:
{"B": null, "A":"String"}
If you change your code to the following - it will work - but because you are essentially manually removing the double quote marks in the payload to get this to work - you might as well just hardcode it - you won't be able to add this to a looping test case - I will explain
Add a single quote character around the word null when you set the property to null so instead of the following:
testRunner.testCase.testSteps["excelToJson"].setPropertyValue("job",null)
log.info testRunner.testCase.testSteps["excelToJson"].getPropertyValue("job")
you change it to
testRunner.testCase.testSteps["excelToJson"].setPropertyValue("job",'null')
log.info testRunner.testCase.testSteps["excelToJson"].getPropertyValue("job")
this will result in the word null being written to the job property value field.
Currently in your payload as B is a string type - your payload looks like:
{"B":"${excelToJson#job}", "A":"String"}
If you remove the double quotes wrapping the parameterisation string so it reads:
{"B":${excelToJson#job}, "A":"String"}
This will then generate the following when you run your REST step:
{"B": null, "A":"String"}
Which is what you want!
HOWEVER - as I said before - the B attribute is a string type - so we've had to remove the double quotes from your payload to ensure null is not wrapped in quotes and because of this - if you have this as a looping test case - it will fail on the next run because your text value for the B attribute will NOT be wrapped in double quotes - so in some ways this approach I've laid out above - doesn't help or add any efficiency - as far as I can tell you might as well just hard code the word null in your payload (due to the fact you need to add and remove double quotes)
I hope I've been clear - I try and lay out my thinking and sometimes I'm not very clear in the way I do this - but the above is the way you can replace "B" : "" with "B": null.
as i say above - id doublecheck whether the requirements actually require to you to submit null string attributes - I've been doing similar testing myself on my previous project and empty string attributes weren't sent as null, then were sent as "" instead - but everywhere is different!
Cheers,
rich