Introduction to the JSON:API spec
View Slide
Marco Otte-Witte@marcoow
simplabs.com@simplabs
http://jsonapi.orghttps://github.com/json-api/json-api/blob/gh-pages/images/jsonapi.png
APIs are everywhere
integrating (micro-)services
integrating with 3rd parties
classic server rendered web apps arebecoming the exception
https://s3-us-west-2.amazonaws.com/s.cdpn.io/68939/angular-logo.pnghttp://emberjs.com/images/brand/ember_Ember-Light.pnghttp://red-badger.com/blog/wp-content/uploads/2015/04/react-logo-1000-transparent.png
http://www.electronicways.com/wp-content/uploads/2014/12/Android-Tablet.jpghttps://cdn2.macworld.co.uk/cmsdata/features/3530504/iphone-7-jet-black.jpg
https://camo.githubusercontent.com/5dd01312b30468423cb45b582b83773f5a9019bb/687474703a2f2f656c656374726f6e2e61746f6d2e696f2f696d616765732f656c656374726f6e2d6c6f676f2e737667https://upload.wikimedia.org/wikipedia/commons/thumb/5/5f/Windows_logo_-_2012.svg/2000px-Windows_logo_-_2012.svg.pnghttps://upload.wikimedia.org/wikipedia/commons/2/22/MacOS_logo_%282017%29.svg
usually JSON via REST**or recently GraphQL
https://twitter.com/thomasfuchs/status/604323589979049984
there's hundred of variations of JSONvia REST
» curl https://api.github.com/repos/rails/railsHTTP/1.1 200 OK…{"id": 1,"name": "sinatra",…}
» curl -i https://api.travis-ci.org/repos/rails/railsHTTP/1.1 200 OK…{"repo": {"id": 82,"slug": "sinatra/sinatra",…}}
» curl https://api.github.com/repos/rails/railsHTTP/1.1 200 OK…{"id": 1,"name": "sinatra",…"owner": {"login": "rails","id": 4223,…}}
» curl -i https://api.travis-ci.org/repos/rails/railsHTTP/1.1 200 OK…{"repo": {"id": 82,"slug": "sinatra/sinatra",…"last_build_id": 23436881,…}}
snake_case or kebap-case?complete updates or partial updates?filtering?sparse field sets?
options, options, optionsopinions, opinions, opinions
root levelkeys!plainhashes!embedrelations!referencerelations!
https://www.broxap.com/media/catalog/product/cache/1/image/9df78eab33525d08d6e5fb8d27136e95/b/i/bikeshed_bxmwmu2.jpg_1.jpg.jpg
JSON API is your "anti-bikeshedding tool"
“ “ “ “
The Spec
Media Typeapplication/vnd.api+jsonhttp://www.iana.org/assignments/media-types/application/vnd.api+json
GET /articles/1 HTTP/1.1Accept: application/vnd.api+json
{"data": {"type": "articles","id": "1","attributes": {"title": "JSON API paints my bikeshed!"}}}HTTP/1.1 200 OKContent-Type: application/vnd.api+json
Resource Objectsrepresent individual resources
GET /articles/1{"data":{"type": "articles","id": "1","attributes": {"title": "JSON API paints my bikeshed!"}}}
GET /articles{"data": [{"type": "articles","id": "1","attributes": {"title": "JSON API paints my bikeshed!"}},{"type": "articles","id": "2","attributes": {"title": "Rails is Omakase"}}]}
GET /articles/1{"data": {"type": "articles","id": "1","attributes": {"title": "JSON API paints my bikeshed!"},"relationships": {"author": {"data":{"type": "people","id": "1"}}}}}
GET /articles/1{"data": {"type": "articles","id": "1","attributes": {"title": "JSON API paints my bikeshed!"},"relationships": {"author": { "data": { "type": "people", "id": "1" } }}},"included": [{"type": "people","id": "1","attributes": {"name": "Dan Gebhard"}}]}
GET /articles/1{"data": {"type": "articles","id": "1","attributes": {"title": "JSON API paints my bikeshed!"},"relationships": {"author": {"links": {"self": "/articles/1/relationships/author","related": "/articles/1/author"}}}}}
CRUDwe're not always reading data after all
POST /articles{"data":{"type": "articles","attributes": {"title": "JSON API paints my bikeshed!"}}}
HTTP/1.1 201 CreatedLocation: http://example.com/articles/1{"data":{"type": "articles","id": "1","attributes": {"title": "JSON API paints my bikeshed!"}}}
PATCH /articles/1{"data":{"type": "articles","id": "1","attributes": {"title": "json:api paints my bikeshed!"}}}
HTTP/1.1 204 No Content
DELETE /articles/1
Inclusion of related resourcesas per client request
GET /articles/1?include=comments.author
Sparse field setsfor smaller response payloads
GET /articles?include=author&fields[articles]=title,body&fields[people]=name
There's moreerrors, filtering, pagination, etc.
Client and Server librariesfor many languages and frameworkshttp://jsonapi.org/implementations/
What's a spec worth if everyone uses adifferent version?
JSON API is strictly additive
"What is he even talking about, we're allhyped up about GraphQL!!!"
REST can be pretty fast
{"data": {"type": "articles","id": "1","attributes": {"title": "JSON API paints my bikeshed!"}}}HTTP/1.1 200 OKContent-Type: application/vnd.api+jsonETag: "686897696a7c876b7e"
GET /articles HTTP/1.1Accept: application/vnd.api+jsonIf-None-Match: "686897696a7c876b7e"
HTTP/1.1 304 Not Modified
HTTP/2more parallel requests, server push, etc.
Uniform resource representationsfacilitate data reuse
GET /articles?include=author{"data": [{"type": "articles","id": "1","attributes": {"title": "JSON API paints my bikeshed!","text": "…"},"relationships": {"author": { "data": { "type": "people", "id": "2" } }}},…],"included": [{"type": "people","id": "2","attributes": {"name": "Dan Gebhard","bio": "…","imageUrl": "…"}},…]}
https://raw.githubusercontent.com/facebook/graphql/master/resources/GraphQL%20Logo.pnghttp://graphql.org
POST /graphql{articles {idtitletextauthor {idname}}}
POST /graphql{author(id: "1") {namebioimageUrl}}
GraphQL opens an endpoint on yoursystem that allows (somewhat) arbitraryqueries to your data
"GraphQL will replace REST in the sameway MongoDB replaced PostgreSQL."Tom Dale
"Things are going to get better foreveryone through experimentation withnew approaches."Marco Otte-Witte
♥
Thanks
Q&A
http://simplabs.com@simplabs