dewski / json_builder

Rails provides an excellent XML Builder by default to build RSS and ATOM feeds, but nothing to help you build complex and custom JSON data structures. JSON Builder is here to help.
http://garrettbjerkhoel.com/json_builder/
MIT License
244 stars 52 forks source link

display collection as json array in rails #47

Closed joshryanne closed 10 years ago

joshryanne commented 10 years ago

hi, i'm very new to rails(and ruby). was wondering how i can get the following output in rails:

[ { "first": "Garrett", "last": "Bjerkhoel" }, { "first": "John", "last": "Doe" } ]

\ if i do something like

Staff @users do |user| first user.firstname last last:lastname end

\ it will give me something like:

{ "staff":[ { "first": "Garrett", "last": "Bjerkhoel" }, { "first": "John", "last": "Doe" } ] }

\ but i don't want to include "staff" etc. i just want a json with an array directly as i mentioned above can you pls. teach me how, as i've said i'm new to ruby and rails. thanks.

dewski commented 10 years ago

You have to use the array method:

array @users do |user|
  first user.firstname
  last user.lastname
end

Welcome to Rails btw!