Sergey Orshanskiy
Sergey Orshanskiy

Reputation: 7054

What JSON to return if no data is available?

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.

Upvotes: 0

Views: 656

Answers (1)

Matt Ball
Matt Ball

Reputation: 359786

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.

Upvotes: 3

Related Questions