Should null values be included in JSON responses from a REST API?

api, json, rest

Solution

There was a discussion about this recently on API-Craft. The general consensus was there is potentially a semantic difference between the omission of a value, versus an inclusion of a null value.

If there is no semantic value to be gained for your specific use case, then I would say look at your target consumers of the API, and think about whether omitting the value will cause them problems.

Problem

I'm in the process of designing and developing a RESTful API. I'm taking a pragmatic, resource oriented approach to the API (resource oriented, uniform interface, addressability, but no real HATEOAS). One point I'm not sure about though is how to approach null values in objects. Should I include fields with null values in the APIs responses? Example: ``` { "fieldA": "AAA", "fieldB": null } ``` Or, should I just leave out these fields altogether if the system has no data for these fields? Example: ``` { "fieldA": "AAA" } ```

Original source

Related problems