Reputation: 166
Novice Question:
I have a rest api in my application : https://hostname/app-rest/session/manager/active-session/count
. The API returns the number of sessions with certain attributes like connection type, status, etc.
When I set the request header Content-Type: application/json
, response header Accept: application/json
and send a request with body like:
{"loggedIn": "yes", connectionType" : "mobile"}
, I get a response with a number 0, 1, 2 etc.
QUESTION: Based on this: http://json.org/, it seems that the response is a valid JSON format. However, is this good design practice? Isn't it preferable to return the value in a key value pair format so that all clients can understand and parse the response - instead of assuming that the client has the knowledge of what is returned by the API ?
Upvotes: 2
Views: 6656
Reputation: 141827
According to the original RFC, a number on its own is not a valid JSON text. JSON must always begin with {
or [
(optionally preceded by some whitespace characters).
There is a new RFC that revises this by allowing any JSON value to be an entire JSON text. If you go by this RFC, then a number on its own is calid JSON.
For the time being I would recommend returning an object, with a key describing the meaning of the number returned, since that will conform to both RFCs.
Upvotes: 3