0

I am looking for a way to add an identifier to my JSON output so it can be more easily parsed. Currently, the output is :

[  
   {  
      "id":9,
      "name":"Test Location",
      "description":"Test Description",
      "address":"123 Fake Street",
      "latitude":-85.0,
      "longitude":-101.10101,
      "created_at":"2015-11-15T21:25:08.643Z",
      "updated_at":"2015-11-15T21:27:23.419Z"
   },
   {  
      "id":10,
      "name":"Test Location",
      "description":"testest",
      "address":"estesets",
      "latitude":1.0,
      "longitude":1.0,
      "created_at":"2015-11-15T22:05:39.224Z",
      "updated_at":"2015-11-15T22:05:39.224Z"
   }
]

The ideal output would be:

{ locations: 
  [  
       {  
          "id":9,
          "name":"Test Location",
          "description":"Test Description",
          "address":"123 Fake Street",
          "latitude":-85.0,
          "longitude":-101.10101,
          "created_at":"2015-11-15T21:25:08.643Z",
          "updated_at":"2015-11-15T21:27:23.419Z"
       },
       {  
          "id":10,
          "name":"Test Location",
          "description":"testest",
          "address":"estesets",
          "latitude":1.0,
          "longitude":1.0,
          "created_at":"2015-11-15T22:05:39.224Z",
          "updated_at":"2015-11-15T22:05:39.224Z"
       }
    ]
}

My current controller is:

module Api
  module V1
    class LocationsController < ApplicationController
unless Rails.env.test?
      before_filter :restrict_access
    end
      respond_to :json

      def index
        @locations = Location.all
        respond_with @locations
      end

      private

      def restrict_access
        api_key = ApiKey.find_by_access_token(params[:access_token])
        head :unauthorized unless api_key
      end
    end
  end
end

I would like for it to have a name of Locations so I can more easily parse it. Thanks for the help!

3
  • What do you mean by 'adding a name of Locations'? Do you want to add extra content to your JSON object? It's not entirely clear what the goal is here. Commented Nov 18, 2015 at 21:57
  • I have edited the post to to show the ideal format Commented Nov 18, 2015 at 22:01
  • You just need to modify your JSON object in such a way that it has a key of 'locations' and a value is going to be the rest of it. Commented Nov 18, 2015 at 22:05

2 Answers 2

2
def index
  @locations = Location.all
  respond_with locations: @locations
end

Results in proper output

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the response, but this results in a SyntaxError: syntax error, unexpected ':', expecting '}' respond_with {locations: @locations} ^
It should be response_with locations: @locations. The curly braces are not required
0

Just work with your @locations.

You can do something like:

@new_locations = {}
@new_locations = {'locations' => @locations}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.