0

Is it appropriate to return an empty JSON structure when no data is available? I feel that it is slightly ambiguous.

I have a server-side interface that allows a client to request some information about a particular stock ticker, and the information (e.g. stock quotes) is returned as JSON. The client will use e.g.

$.getJSON( "http://some.url/stock.json?ticker=MSFT", function( data ) { ...

to get information about Microsoft stock.

Now, what if I have no information about a particular stock? Is it cleaner to return an empty JSON structure or should it be HTTP 404? If I return an empty JSON, does it mean that there is no such ticker at all, if e.g. the client requested http://some.url/stock.json?ticker=ANOTHERMSFT, or that simply there is no data for a particular ticker? I am the one implementing the client, so it doesn't matter that much; just curious, what is a better approach and why.

Sergey Orshanskiy
  • 6,794
  • 1
  • 46
  • 50
  • I've actually found a similar discussion: [REST API 404: Bad URI, or Missing Resource?](http://stackoverflow.com/questions/9930695/rest-api-404-bad-uri-or-missing-resource) – Sergey Orshanskiy Oct 08 '13 at 20:12

1 Answers1

3

As much I encounter similar questions when designing HTTP APIs, and so I feel your pain, there is no right answer. It's up to you to make these subjective API design decisions.

My $0.02: your URL structure is not terribly restful; I'd use something like http://some.url/stock/tickers/MSFT. Then, perhaps more naturally, 404 means that no such ticker exists while an empty object/array means there is no data for that ticker.

Matt Ball
  • 354,903
  • 100
  • 647
  • 710