How to response with JSON format using Ruby Rack middleware How to response with JSON format using Ruby Rack middleware json json

How to response with JSON format using Ruby Rack middleware


Include the "json" gem in your project, and then call #to_json on the Hash:

app = Proc.new do |env|   [200, { 'Content-Type' => 'application/json' }, [ { :x => 42 }.to_json ]]end

Note that nil is translated to null in the JSON, if you need null.