I'm making an API that will return data in JSON.

I also wanted on client side to make an utility class to call this API.

Something like :

JSONObject sendGetRequest(Url url);
JSONObject sendPostRequest(Url url, HashMap postData);

However sometimes the API send back array of object [{id:1},{id:2}]

I now got four choices ():

  • Make the method test for JSONArray or JSONObject and send back an Object that I will have to cast in the caller
  • Make a method that returns JSONObject and one for JSONArray (like sendGetRequestAndReturnAsJSONArray)
  • Make the server always send Arrays even for one element
  • Make the server always send Objects wrapping my Array

I going for the two last methods since I think it would be a good thing to force the API to send consistent type of data.

But what would be the best practice (if one exist).

Always send arrays? or always send objects?


That should depend on what it is actually doing. If it is retrieving one or more objects, it makes logical sense to always return an array containing the results, even if it is an array with only one element. This makes it easy to then do a foreach on the returned array, for example.

If the format of the request causes it to return completely different kinds of data though, it would be clearer to have a different method for each context.

How this would work as a method is a good analogy. If you have some collection foo, and a function searchFoo that searches the collection for elements, you are not going to write it such that it returns an element if only one element matches, and otherwise it returns a list of elements. Either it always only finds one elements (like if the search is based on a unique key) in which case it also returns an object of the appropriate type, or it will find a variable number of elements in which case it will always return a list, regardless of how many elements were found.

  • Yeah, I think that's the way I'll be taking. I'm glad to have an opinion about my question. – Michael Laffargue Aug 20 '14 at 19:05

This topic may already be closed but for the sake of dicussion, I will put my 10 cents thought in here. I'm doing a research at json and Web Service security and I think you should always return a json object instead of json array. The reason for this is that json array can be tampered with to return some XSS script. For example: if the array return is

[1, alert('You are hacked!'), 'abc']

Then guess what surpise you will get when the call is finished? It can be treated as a valid javascript call and show the alert. Returning json object will prevent this:

{'data':[1, alert('you are hacked', 'abc')]}

is a non-valid json data and neither js script which will show up as an error in the browser console. That is why calling webservice from asp.net will always return json with a 'd' member.

More info: http://encosia.com/a-breaking-change-between-versions-of-aspnet-ajax/

  • 2
    I don't see why this should increase security. Whatever you get from the server is a string and you can JSON.parse() it. Since your first example is also not valid JSON, parsing will fail: e.g. JSON.parse("[1, alert('You are hacked!'), 'abc']") – TmTron Oct 6 '20 at 8:39
  • 1
    @TmTron Thanks for the comments, this was a 4 years old answer and things have changed a lot. JSON is now being used everywhere in many API thus I don't think this original question is still relevant to ask. However, I should update my answer that this is only true when you are calling the API from a webpage using XMLHttpRequest or via jQuery. Who still use jQuery nowadays right? – Tran Nguyen Feb 23 at 12:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.