{"id":24505813,"url":"https://github.com/michaeltelford/rack_jsonparser","last_synced_at":"2026-05-21T10:02:27.817Z","repository":{"id":56890159,"uuid":"102986166","full_name":"michaeltelford/rack_jsonparser","owner":"michaeltelford","description":"Rack middleware for processing JSON requests and responses","archived":false,"fork":false,"pushed_at":"2019-06-17T03:31:43.000Z","size":14,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-06T01:18:36.107Z","etag":null,"topics":["json","rack","rack-application","rack-jsonparser","rack-middleware","ruby"],"latest_commit_sha":null,"homepage":"https://rubygems.org/gems/rack-jsonparser","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/michaeltelford.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-09-09T21:31:27.000Z","updated_at":"2019-06-01T09:19:06.000Z","dependencies_parsed_at":"2022-08-21T00:50:24.745Z","dependency_job_id":null,"html_url":"https://github.com/michaeltelford/rack_jsonparser","commit_stats":null,"previous_names":["michaeltelford/rack-jsonparser"],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/michaeltelford%2Frack_jsonparser","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/michaeltelford%2Frack_jsonparser/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/michaeltelford%2Frack_jsonparser/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/michaeltelford%2Frack_jsonparser/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/michaeltelford","download_url":"https://codeload.github.com/michaeltelford/rack_jsonparser/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243707299,"owners_count":20334615,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["json","rack","rack-application","rack-jsonparser","rack-middleware","ruby"],"created_at":"2025-01-21T23:31:49.596Z","updated_at":"2026-05-21T10:02:27.725Z","avatar_url":"https://github.com/michaeltelford.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Rack::JSONParser Middleware\n\nRack middleware which transforms an incoming request's JSON string into a `Hash` and any outgoing ruby `Object` back into a JSON string to be returned to the client.\n\n- The client sends/receives only JSON strings\n- The `app` (rack application) using the middleware sends/receives only ruby objects (no JSON parsing)\n- The `Rack::JSONParser` middleware handles the parsing of the request/response to/from JSON/Object so you don't have to\n\nThe difference between this and other middleware doing the same job is that it's fast! This middleware uses the '[oj](https://github.com/ohler55/oj)' gem which gives faster processing than the `json` gem. This middleware is also configurable enabling predictive and controlled parsing. It stays out of your way when you don't need it and is seamless when you do.\n\n## Installation\n\nAdd this line to your application's `Gemfile`:\n\n```ruby\ngem 'rack-jsonparser'\n```\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself with:\n\n    $ gem install rack-jsonparser\n\nThen `require` it in your rack application with:\n\n```ruby\nrequire 'rack/json_parser'\n```\n\n## Usage\n\n### Request\n\nThe incoming requests JSON body will be available in `env['request.payload']`.\n\n### Response\n\nAs per the rack interface your app should return an array containing the status, headers, body. This doesn't change; just remember to include the header `Content-Type: application/json`. The response body can be:\n\n- `String`: Treated as an already valid JSON string and is not processed by the middleware\n- `Hash`: The recommended type of ruby object to return, will be processed into a JSON string and sent back to the client\n- `Object`: Any ruby object can be returned, the resulting JSON will depend on the `Object#to_s` output\n\n**Note** that the above objects should **not** be encased in an `Array` just to comply with the Rack interface. The middleware will automatically encase the generated JSON `String` into an `Array` so it complies with Rack.\n\n### Example\n\nBelow is a sample `rack` app which uses the middleware to send and receive JSON:\n\n\u003e config.ru\n\n```ruby\nrequire 'rack/json_parser'\n\n# Notice how the `request.payload` is a Hash, not a JSON string\n# We return a Hash instance (or any Ruby object) for the response body,\n# no need to encase in an array, the middleware will do this.\n# We can turn off the request/response parsing via the `use` method (defaults to true)\n# Parsing will only occur if enabled AND the `Content-Type` is `application/json`\nhandler = proc do |env|\n  payload = env['request.payload']\n  full_name = payload['forenames'].push(payload['surname']).join(' ')\n  res_hash = { 'full_name' =\u003e full_name }\n  [200, { 'Content-Type' =\u003e 'application/json' }, res_hash]\nend\n\napp = Rack::Builder.new do\n  use Rack::JSONParser, transform_request: true, transform_response: true\n  map('/hello') { run handler }\nend\n\nrun app\n```\n\nRun the above `config.ru` rack app with:\n\n    $ rackup\n\nThen query the app with:\n\n```shell\ncurl -X POST \\\n  http://localhost:9292/hello \\\n  -H 'content-type: application/json' \\\n  -d '{\n\t\"forenames\": [\n\t\t\"Napolean\",\n\t\t\"Neech\"\n\t],\n\t\"surname\":\"Manly\"\n  }'\n```\n\nAnd the JSON response will be:\n\n```json\n{\n  \"full_name\":\"Napolean Neech Manly\"\n}\n```\n\n## Configuration\n\nThe middleware is configurable so request/response processing can be turned off via the `use` method parameters if desired, see usage above.\n\nIn addition, the middleware only transforms the request/response if the `Content-Type` header is correctly set to `application/json`. This applies to both the request sent from the client and the response sent from the `app`; allowing for several types of response content to be served by the application without interference by the middleware. This however requires the client and rack `app` to set the `Content-Type` header correctly. If not, then the middleware will do nothing and simply pass through the request/response data which might lead to unexpected behavior.\n\nIf the middleware processes the response then it will also set/override the `Content-Length` header with the length of the JSON string being returned to the client.\n\n## Ruby Version\n\nThis software was built with and supports:\n\n- `~\u003e 2.4.0`\n\n## Contributing\n\nBug reports and pull requests are welcome on [GitHub](https://github.com/michaeltelford/rack_jsonparser).\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmichaeltelford%2Frack_jsonparser","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmichaeltelford%2Frack_jsonparser","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmichaeltelford%2Frack_jsonparser/lists"}