✎ Edit on GitHub


JSON is an integral part of Vapor. It powers Vapor's Config and is incredibly easy to use in both requests and responses.


JSON is automatically available in request.data alongside form-urlencoded data and query data. This allows you to focus on making a great API, not worrying about what content types data will be sent in.

drop.get("hello") { request in
    guard let name = request.data["name"]?.string else {
        throw Abort.badRequest
    return "Hello, \(name)!"

This will return a greeting for any HTTP method or content type that the name is sent as, including JSON.


To specifically target JSON, use the request.json property.

drop.post("json") { request in
    guard let name = request.json?["name"]?.string else {
        throw Abort.badRequest

    return "Hello, \(name)!"

The above snippet will only work if the request is sent with JSON data.


To respond with JSON, simply wrap your data structure with JSON(node: )

drop.get("version") { request in
    return try JSON(node: [
        "version": "1.0"


The JSONMiddleware is included in the Droplet's middleware by default. You can remove it if you don't want JSON to be parsed.