JSON specification for empty objects

This question is more related to standards and conventions and is closely related to this.

We had a JSON contract established between two different groups, namely UI team dealing with React based framework and Server team developing REST services in Java. The one thing that was not captured is what should be sent and retrieved for 'empty' objects. I would like to know if there are any known JSON conventions which mandates empty object to be sent in certain ways.

Here is an example of what I'm referring to

Sample JSON

'Vehicle': {'Make': 'BMW', 'Model': 'Enclave', 'Year' : '2017' }

When an empty object like

'Vehicle' : {}

is sent from UI application to REST services, I am of the opinion, it is fair to expect the same object back. The server team complains that due to the nature of the service/JSON mapping, when an empty object is sent, the returned object will have null attributes like below:

'Vehicle' : {'Make' : null, 'Model' : null, 'Year' : null} 

Unfortunately this is an area where clear definition of empty objects was not captured during the template agreement. The server team claims this is what is defined in JSON spec, which is not clear from http://www.json.org/

Additional information about the contract:

  • The REST services are for storage and retrieval of the JSON objects and is not expected to make any modifications to the JSON object.
  • There is also no calculation, aggregation or translation of any sort that is expected from the REST services.
  • The JSON is sent to additional downstream systems from the middleware services(Server team).
  • Additional information about the implications of this problem:

  • UI application(React based) has issues in merging null values from JSON into immutable structure(for UI consumption). Empty objects are very well handled, however objects with null attributes requires additional overhead.
  • REST services are developed in Java due to which the empty objects are always returned with attributes having null values.
  • Question:

    Is it correct for the server team to refer to JSON specification and claim 'when empty objects are sent to REST services(without attributes), it will always be sent back with attributes having null values?

    Note: I am not looking for help with the problems faced in UI application or how to achieve the requisite JSON structure in REST services.


    There is no "specification" for what you are asking. It is simply a matter of agreement between server and client. It is neither "correct" nor "incorrect" for the server team to specify that their code returns null for non-existent attributes. However, it is indeed "incorrect" for them to claim that the JSON specification--which merely describes JSON syntax, not how it is used--demands that things work this way.

    however objects with null attributes requires additional overhead.

    I am slightly confused by this statement. If your downstream logic has trouble with null values, then it would seem to be trivially easy and inexpensive to filter out null property values from an object.

    链接地址: http://www.djcxy.com/p/20800.html

    上一篇: 硬拷贝vs浅复制JavaScript

    下一篇: 空对象的JSON规范