skip to Main Content

The following json text is the result of a call to eBay’s search API.

Q: Is this json badly constructed? (It’s technically, correct – so that’s not what I’m referring to)

By that I mean, notice how -every value- is inside an array? instead of just being a "key" : "value" ?

eg.

"ack": [
        "Success"
      ],

or

"version": [
        "1.12.0"
      ],

etc..

Now, before you answer "well, maybe each key has multiple result values" .. I’m pretty sure most of them can’t.

(this json schema is really making my life a pita. yes, it’s easy to make each POCO property a List<string> but it’s the point of it all!)

References: Here’s the office eBay API documention for this endpoint

2

Answers


  1. I normally use this tool for see if a JSON is good constructed:

    http://json.parser.online.fr/

    In your case, is not correct. You should store the data by Keys and Values.

    Login or Signup to reply.
  2. After reading the documentation, I understand eBay’s approach, but I find it poor in terms of client side deserialisation. For example, the benefit of an array value for the ack property is that the value could also contain a warning, e.g.:

    {
        "ack": [
            "Success",
            "Warning"
        ]
    }
    

    However, a list of strings is not ideal for client side processing (e.g. in C#, bool hasWarning = pocoList.Contains("Warning"); doesn’t strike me as completely foolproof). I’d rather have a response such as:

    {
        "ack": {
            "result": "Success",
            "warning": null
        }
    }
    

    Then with my deserialised POCO, given that the value of warning is still a string, I could write this:

    [DataContract]
    public class Ack
    {
        [DataMember(Name="result")]
        public string Result { get; set; }
    
        [DataMember(Name="warning")]
        public string Warning { get; set; }
    
        [IgnoreDataMember]
        public bool HasWarning
        {
            get { return !string.IsNullOrEmpty(Warning); }
        }
    }
    

    This would allow me to replace my previous LINQ query with bool hasWarning = ack.HasWarning;.

    There are definitely places where use of arrays is completely unnecessary. The docs describe the version property as “the release version that eBay used to process the request”, thus I would return this as a single string. The array would only make sense if it was a versions property (e.g. for identifying all versions of the backend that support a specific request).

    I’ve definitely seen worse JSON responses, but there are places where the APIs should ideally be returning a JSON object or a single value.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search