I will receive the following JSON request to my service.
{
"city" : "Hyderabad",
"state" : "Telangana",
"country" : "India"
}
Sometimes In the request, I might not get the city field or city field might be empty which is not expected. So, I'm handling that in the following way:
payload is my request JSON.
if ( payload.has("city") && !payload.get("city").equals("") )
I'm handling it in the above way. But the problem is that If I add new mandatory again then I need to add two more conditions as:
if ( payload.has("city") && !payload.get("city").equals("") && payload.has("newKey") && !payload.get("newKey").equals("") )
- check whether the key is available or not.
- check whether the value is not empty.
Is there any best practice to solve this?