Create a New JSON format from another JSON in Ruby on Rails

Viewed 20

I'm new to ruby on rails and I'm trying to manipulate the json data into another json format. I have this Controller,thats calls API service URL: https://api.publicapis.org/entries

class DemoController < ApplicationController
    def index
        demohelper = DemoHelper::APIService.new
        obj = JSON.parse(demohelper.getdata())
        @entrylist = obj['entries'].map do |e|
           e
        end
        render json:@entrylist
    end
end

below is the sample data from the entries array

API :   WeatherAPI
Description :   Weather API with other stuff like Astronomy and Geolocation API
Auth    :   apiKey
HTTPS   :   true
Cors    :   yes
Link    :   https://www.weatherapi.com/
Category    :   Weather

what I would like to return is

[{ api, desc }]

but when I use the code below

@entrylist = obj['entries'].map do |e|
  { :api => e.API , :desc => e.Description }
end

but I keep on getting error when I use the code above

enter image description here

-----UPDATE ----

I can actually use the rocket symbol by doing the field as "api" not :api as I previously used.

@entrylist = obj['entries'].map do |e|
  { "api"=> e["API"] , "desc"=> e["Description"] }
end
1 Answers

Just need to reference the API/Description methods as keys, not methods:

@entrylist = obj['entries'].map do |e|
  { api: e["API"] , desc: e["Description"] }
end
Related