Home > Enterprise >  API best practices on responses
API best practices on responses

Time:08-13

I'm exposing an API but some queries do not return values for some keys, should they still be returned with NULL/EMPTY or not returned at all ?

"Name":"Thales"
"City": ""
"ZIP": NULL

Is there a best practices on this matter?

CodePudding user response:

It really depends on the client implementation. If it is written on a strongly typed language like java, then it might not be able to parse the response, because some of the properties are missing. On the other hand a javascript client would not care at all.

I have the impression that usually these JSON based clients can be configured to ignore these kind of processing errors, for example here Ignore missing properties during Jackson JSON deserialization in Java it can be turned off. So I would not send null if the property is empty.

CodePudding user response:

It all comes down to how the client expects to handle null values.

Always returning the key even when the value is null, allows the client code to directly reference the key without a prior ifKeyExists check.

  • Related