Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: change default JSON structure

I'm new in Rails and I use the version 4.2.0.

Let's suppose that we have the following JSON at the url localhost:3000/users

[{id: 1, "firstName": "John","lastName": "Smith"},
{id: 2, "firstName": "John","lastName": "Red"}]

but this is not what I want, I expect something like:

{ users :[
{id: 1, "firstName": "John","lastName": "Smith"},
{id: 2, "firstName": "John","lastName": "Red"}
]}

How can i do? Thanks everybody.

like image 687
Francesca Avatar asked Dec 05 '25 08:12

Francesca


1 Answers

try this in users index controller

render json: {users: @users}

where

@users = [{id: 1, "firstName": "John","lastName": "Smith"},
          {id: 2, "firstName": "John","lastName": "Red"}]
like image 198
pkrawat1 Avatar answered Dec 07 '25 21:12

pkrawat1



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!