Slide 1

Slide 1 text

JSON API standards please Michał Ostruszka | @mostruszka | michalostruszka.pl

Slide 2

Slide 2 text

every app defines own “standard” Wild, wild west

Slide 3

Slide 3 text

{ “status”: “ok”, “response”: {...} }

Slide 4

Slide 4 text

{ “result”: “success”, “data”: [] }

Slide 5

Slide 5 text

{ “status”: 200 }

Slide 6

Slide 6 text

{ “result”: “error”, “msg”: “No such user” }

Slide 7

Slide 7 text

{ “status”: 404, “error”: “No such user” }

Slide 8

Slide 8 text

{ “status”: 404, “errors”: [ “No such user” ] }

Slide 9

Slide 9 text

and so on...

Slide 10

Slide 10 text

sometimes even within one app Inconsistency

Slide 11

Slide 11 text

Any standards?

Slide 12

Slide 12 text

“JSend is a specification that lays down some rules for how JSON responses from web servers should be formatted” JSend

Slide 13

Slide 13 text

JSend - success { “status”: “success”, “data”: { “posts”: [...] } } required: status, data

Slide 14

Slide 14 text

JSend - failure { “status”: “fail”, “data”: { “title”: “Title is required” } } Used for invalid data responses required: status = “fail”, data

Slide 15

Slide 15 text

JSend - error { “status”: “error”, “message”: “Server goes bananas” } Used for server errors required: status = “error”, message optional: code, data

Slide 16

Slide 16 text

simple easy to follow, “data” can contain anything That’s all

Slide 17

Slide 17 text

only 41 various interpretations not always flexible enough Wait, HTTP codes?

Slide 18

Slide 18 text

“If you've ever argued with your team about the way your JSON responses should be formatted, JSON API is your anti-bikeshedding weapon.” JSON API

Slide 19

Slide 19 text

defines data format, mime type registered application/vnd.api+json relations etc Bigger spec

Slide 20

Slide 20 text

ID style URL style Flavors

Slide 21

Slide 21 text

JSON API - Simple { “posts”: [{ “id”: 1, “title”: “Hello world” }] } always as list, even for 1 item item MUST contain “id”

Slide 22

Slide 22 text

JSON API - Relationships { “posts”: [{ “id”: 1, “title”: “Hello world”, “links”: { “author”: “9” “comments”: [“1”,”2”,”3”] } }] } Client needs to know how to build author url

Slide 23

Slide 23 text

Embedded in “linked” field Save HTTP requests Compound docs

Slide 24

Slide 24 text

Same for simple resources URL Style

Slide 25

Slide 25 text

URL Style { “posts”: [{ “id”: 1, “title”: “Hello world”, “links”: { “author”: “http://example.com/author/2” } }] } client can blindly follow URLs

Slide 26

Slide 26 text

GET /posts/1?include=comments GET /posts/1?include=comments.author Selective fetching

Slide 27

Slide 27 text

Defines how to send data POST, PUT, DELETE etc Resources manipulation

Slide 28

Slide 28 text

No status in response body HTTP codes used to determine status Relies on HTTP codes

Slide 29

Slide 29 text

Once JSON API is stable, it will always be backwards compatible using a never remove, only add strategy Changes?

Slide 30

Slide 30 text

http://labs.omniti.com/labs/jsend http://jsonapi.org/ Where to find?

Slide 31

Slide 31 text

Possible adoption? Stick to standard for your project JSend looks easier Final word

Slide 32

Slide 32 text

kthxbye