Say in my application, some users give us their last name, and others do not. In a REST API response, which body is preferred:
With a "null" value:
{"firstName": "Bob",
"lastName": null}
Or just a missing key:
{"firstName": "Bob"}
Say in my application, some users give us their last name, and others do not. In a REST API response, which body is preferred:
With a "null" value:
{"firstName": "Bob",
"lastName": null}
Or just a missing key:
{"firstName": "Bob"}
If a property is optional or has an empty or null value, consider dropping the property from the JSON, unless there's a strong semantic reason for its existence.
{
"volume": 10,
// Even though the "balance" property's value is zero, it should be left in,
// since "0" signifies "even balance" (the value could be "-1" for left
// balance and "+1" for right balance.
"balance": 0,
// The "currentlyPlaying" property can be left out since it is null.
// "currentlyPlaying": null
}
Further Reading
Google Style Guide - Empty or Null Property Values
Should null values be included in JSON responses from a REST API?