- edited description
Ignore empty values in API response
API response contains full set of properties even if most of them are empty(for example, history and animelist requests). I think it is better to ignore empty values(easier to read + less outgoing traffic). Could you please change serialization settings?
It is easy to do in .NET and I guess it should be possible in PHP too. :)
Comments (9)
-
reporter -
What API version are you calling? API version 1.0 explicitly sends nulls for compatibility while 2.0 and above do not serialize null values.
-
reporter I call 2.1 version. And it has empty values. (Can't check the same request on your API instance because of 403 error. I call it from Firefox browser, if it is important.)
-
According: the google JSON styling guide
{ "canPigsFly": null, // null "areWeThereYet": false, // boolean "answerToLife": 42, // number "name": "Bart", // string "moreData": {}, // object "things": [] // array }
It depends what you call empty. Null values are not visible in 1.0+ versions. Empty Arrays, strings, objects are visible.
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.
Note integers are an exception for that line.
A value can be a string in double quotes, or a number, or true or false or null, or an object or an array. These structures can be nested.
Dropping the empty arrays could mean for endpoint users:
- There is no array of this type on the page
- This part is broken in the API
- The array is empty
- This array has been removed from the code
- This is not an array but an empty object or another type.
- This is an empty array which contains empty objects which can contain arrays (A parent array)
Leaving the empty arrays could mean:
- This part might broken in the API
- The array is empty
This is the way how I understand this. I might be wrong.
-
reporter I did not explain the problem well and I want clarify it. By empty I mean property which is not provided by the API method. For example, anime objects from user's animelist request do not have genres, alternative titles, op&ed and so on. In other words, I suggest to ignore properties which are not provided by the API method.
-
I'm now more confused. Can you provide an example so I can see the issue?
-
reporter I've marked some of the properties using red rectangles. Every collection element have these properties, but they are not filled and so they are useless. If possible, I would like to avoid downloading these key-value pairs that actually does not have any content.
Comment preview does not show me attached picture. If you can't see it, click here. (image hosting)
-
Attaching screenshot directly to issue.
-
I see what you mean. The thing is, they are just empty arrays, and the current settings are to send objects across. Since it's using the same record format (to make parsers easier to write), it will include empty arrays since that data isn't available but is part of the record format.
We may consider changing this, but it won't happen until a new major API version because of the potential for breakage.
- Log in to comment