Hot answers tagged

5

Whether you choose the first or the third option depends on your use case. If you are modeling many different instances of the same type of thing, choose the first. For example, you have a list of people. If you are modeling many different attributes of one thing, choose the third. You can have repeated keys in the first format, but not in the third. ...


3

GET /api/sprinkler that returns 200 and {"status": "on"/"off"/"damaged"/"no water".....} In my view assigning meaning to error codes which already have a defined meaning is a bad idea. How would you distinguish between "off" sprinklers and "client pointing at the wrong url" exceptions?


2

For your case, I believe returning { "status" : "off" } With a 200 status code is "correct". In practice, it doesn't matter much. When Fielding published his famous rant on hypertext, he called out a particular error in this way: Failure here implies that out-of-band information is driving interaction instead of hypertext. Out-of-band in this ...


2

From my understanding, the solution 1 misunderstood the semantics of the HTTP status code. From RFC HTTP/1.1: Status code Definitions 10.4 Client Error 4xx The 4xx class of status code is intended for cases in which the client seems to have erred. In you example, it's a matter of resource representation. Because you are probably thinking that a ...


1

The proper HTTP error code on input is 400: Bad Request. In the response you could go with 500. If there is an error in marshalling or unmarshalling, an exception will be thrown which you can handle by registering an ExceptionMapper (scoll down to the Exception Mapping section). You can then determine what kind of error to throw. The JAX-RS package has a ...


1

You say these are key / value pairs. In that case, use #3: dictionary of key / value pairs. If these are not key / value pairs, then don't call them "keys" and "values" and use #2, an array of dictionaries with arbitrary contents. Structure #1 is just daft unless you need key / value pairs but also their order. Which you rarely do.


1

The 3rd format is in general the best. However, here's an example of something suitable for the 1st format: [{ "username": "foo", "id": 1 }, { "username": "bar", "id": 2 }, { "username": "baz", "id": 3 }] Here each object refers to a separate thing (and each object uses the same keys as the others, so they couldn't be merged). However, each ...


1

Put your self in the shoes of the person receiving the json. If i don't know the key name how am i supposed to retrieve it with the first or the last format? You can loop through the json of course but since all three formats achieve the same result it's just a question of syntax. I think this is the only meaningful question: if you know the key names the ...


1

Disclaimer: This answer disagrees with the current accepted answer. Keep an open mind and see why. We're all just trying to share knowledge here anyway right? Skip to end for TL;DR For anyone stumbling upon this now, this answer tries to stick to the design pattern of MVC in context of the web defined simply below: Model: the data persistence layer View: ...



Only top voted, non community-wiki answers of a minimum length are eligible