Ignore empty values in API response

Issue #112 new
Toco Toucan created an issue

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)

  1. Michael Johnson

    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.

  2. Ratan Dhawtal

    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.

    Empty/Null Property Values

    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.

  3. Toco Toucan 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.

  4. Michael Johnson

    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.

  5. Log in to comment