Ask a Question

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

SOLVED
hemantloya
New Contributor

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 B both are mandatory field.

 

Case 1

I want to pass below json

{ "A" : "Jhon" , "B": null} 

 

Case 2

I want to pass below json

{ "A" : "Jhon" , "B": ""} 

 

Case 3

I want to pass below json

{ "A" : "Jhon" } 

 

All three request are acceptable and once we post will say that "Filed B is missing as it is a mandatory parameter". Now I do not want to create three different request for above because my actual request would have lot many mandatory fields so cannot keep on creating different test request with respect to each field.

 

How do I solve above in a same request such that i get the data from Excel for both fields and at the same time i can pass null, blank in the field B and in third case I do not pass the field it self.

 

Thanks

Hemant

 

1 ACCEPTED SOLUTION

Accepted Solutions
hemantloya
New Contributor

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

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

View solution in original post

6 REPLIES 6
richie
Community Hero

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

Hi @Hemant,

Someone else might have a better opinion, and i could be misunderstanding, but if you only want to submit a single request for those e individual tests, that's impossible. Its cos you'll need 3 separate requests to test that for the simple reason you'll have different results and so you'll have different assertions.

I myself have a payload with about 60 attributes in it. Many are mandatory and some are optional and i've had to create an individual request for each of the negative test scenarios (i.e. 1 request for each attribute that has a mandatory attribute either blank "" (for string), null (for numerics and boolean) along with removing the attribute itself.

Cheers,

Rich
if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta
hemantloya
New Contributor

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

Hey @richie ,

 

Thanks for the reply. So Is there any way to pass NULL in a field B using data-parametrization ??

 

I tried below in the groovy script.

testRunner.testCase.testSteps["excelToJson"].setPropertyValue("job",null)
log.info testRunner.testCase.testSteps["excelToJson"].getPropertyValue("job")

 

excelToJson ==> is a properties step and it has property named job.

now I use this property value in a field B of POST request in below manner.

 

{"B":"${excelToJson#job}", "A":"String"}

when we run the test case , rather than passing NULL to field B , value passed in BLANK and actual request sent out is  {"B": "", "A":"String"}. So not sure how do we transfer NULL value in the request.

 

 

Steps in test case are in below sequence

1) groovy 

2) excelToJson (properties step)

3) Post Request

 

Thanks

Hemant

richie
Community Hero

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

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

 

 

if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta
hemantloya
New Contributor

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

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

View solution in original post

nmrao
Community Hero

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

Just note that null is different from "null"


Regards,
Rao.
sonya_m
Community Manager

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

Thank you for the valuable input, richie, mnrao! 

 

Hi @hemantloya, I’m glad you’ve found the solution you were looking for! Great job. 


Sonya Mihaljova
Community and Education Specialist

Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️
cancel
Showing results for 
Search instead for 
Did you mean: