ruby-on-railsarraysrubyruby-on-rails-5grape-api

Return nested array with Ruby Grape in Rails 5


I have successfully built an api as you can see here and the code is below. I need to list the objects as a nested array and not an array of objects, but I cannot figure it out.

api/v1/plaques.rb

module API  
    module V1
      class Plaques < Grape::API
        include API::V1::Defaults

        resource :plaques do
          desc 'Return all Plaques'
          get '', root: :plaques do
            Plaque.all
          end

          desc 'Return a Plaque'
          params do
            requires :id, type: String, desc: 'ID of Plaque'
          end
          get ':id', root: 'plaque' do
            Plaque.where(id: permitted_params[:id]).first!
          end
        end
      end
    end
  end 

What I'm getting:

[
    {
    "veteran_first": "Alexis",
     ...
    }
]

What I need:

 "items" =  [
        {
        "veteran_first": "Alexis",
         ...
        }
    ]

Solution

  • This code returns JSON you want:

    get '', root: :plaques do
      { items: Plaque.all }
    end