{"title":"JSON Mapping","description":"","section":"cookbook","version":"v1.5","path":"cookbook/json-mapping","canonical_url":"https://amberframework.org/docs/v1.5/cookbook/json-mapping","markdown_url":"https://amberframework.org/docs/v1.5/cookbook/json-mapping.md","inherited":true,"content_markdown":"# JSON Mapping\n\nThis recipe will help you to setup a basic JSON Mapping in your application.\n\n{% hint style=\"warning\" %}\nFirst you need an amber project generated with [Amber CLI](../guides/create-new-app.md) or [from scratch](from-scratch.md).\n{% endhint %}\n\nJSON requests are automatically parsed into the `params` macro when the `accept` header is present and with `application/json` &#x20;\n\nYou can use this in combination with the [`respond_with`](../guides/controllers/respond-with.md) helper. Here you don't need to setup `content_type`, however, the requested path requires a `.json` extension, by example `/json_mapping.json`\n\n```crystal\nclass SomeController < ApplicationController\n  def json_mapping\n    return \"empty body\" if params[\"some_json_key_from_your_request\"]\n    user = User.from_json request.body.to_s\n    user.username += \"mapped!\"\n    response_with do\n      json user.to_json\n    end\n  end\nend\n```\n\nAlso see [Response With](../guides/controllers/respond-with.md) and [Response & Request](../guides/controllers/request-and-response-objects.md)."}