Reputation: 828
Am having some trouble with a Rails Serializer in an API only app. The app is returning a javascript object where the keys are not enclosed in double quotation marks. Of note, the source API is returning a javascript object with keys not surrounded by quotes.
I've found two posts that seem to be related. The second answer here notes that if you receive an object that's not a string, you don't need to parse it, so I guess that's what's going on here, but I can't figure out what to use in place of JSON.parse response
in my model. Another related post here; as suggested there, I've tried return JSON(response)
instead of JSON.parse response
, which also did not produce valid JSON. I see I can remove the each_serializer: KoboApiSerializer
line in my Controller, and have tried doing that with the `return JSON(response) line in the Model.
Any suggestions on what I should do to produce a valid JSON result would be appreciated.
Model, Controller, returned JSON, and source JSON returned from API below. I also have included what is puts
'd out to my terminal via the line in my controller. Thanks in advance.
Model:
class KoboApi < ActiveRecord::Base
require 'rest_client'
USERNAME = ENV['KOBO_API_USER']
PASSWORD = ENV['KOBO_API_PWD']
SURVEY = ENV['KOBO_API_SURVEY']
API_BASE_URL = ENV['API_BASE_URL']
def self.get_api_info
uri = "#{API_BASE_URL}/data/#{SURVEY}?format=json"
rest_resource = RestClient::Resource.new(uri, USERNAME, PASSWORD)
response = rest_resource.get
JSON.parse response
end
end
Controller:
class KoboDataController < ApplicationController
def index
@kobo_info = KoboApi.get_api_info
puts "@kobo_info: #{@kobo_info.inspect}" #values for this returned below. this shows keys surround with quotes.
@kobo_info.each do |kobo_info|
#some records have NULL for the month_and_year date field. this if then statement
date = if kobo_info['month_and_year']
Date.parse(kobo_info['month_and_year'])
else
nil
end
KoboApi.where(lemurs_quantity: kobo_info['lemurs_quantity'], month_and_year: date, lemur_category: kobo_info['lemur_category'], location_admin1: kobo_info['location_admin1'], location_admin2: kobo_info['location_admin2']).first_or_create
end
render(
json.KoboApi.all,
each_serializer: KoboApiSerializer
)
end
end
example of object returned from my Rails API app
A serializer is in place (here "kobo_data"), so objects are grouped under that. Am including an example object from those returned - note that the object here doesn't necessarily match those I've posted from source API or from console - am including it to show that keys are not enclosed in quotations.
{
kobo_data: [
{
lemurs_quantity: 20,
month_and_year: "2016-03-01",
lemur_category: "indri",
location_admin1: "toliara",
location_admin2: "androy"
}
]
}
example of source json from API:
One object (of the many) returned from the source API. Values here don't match values those above or below - am just including to show that keys are not enclosed in double quotations.
[
{
location_admin1: "fianarantsoa",
location_admin2: "atsimo-atsinanana",
lemurs_quantity: "5",
month_and_year: "2016-01-01",
lemur_category: "brown_lemur",
},
]
Putsing it to the terminal....
Values puts
'ed out to my terminal via the line in my controller show that it appears to be a valid JSON object, with keys in quotations. As above, the values here do not match the values from my source data - this is just to show that returned values here are enclosed in quotations.
Processing by KoboDataController#index as HTML
@kobo_info: [{"location_admin1"=>"fianarantsoa", "location_admin2"=>"atsimo-atsinanana","lemurs_quantity"=>"5", "month_and_year"=>"2016-01-01","lemur_category"=>"brown_lemur"}
KoboApi Load (0.3ms) SELECT "kobo_apis".* FROM "kobo_apis" WHERE "kobo_apis"."lemurs_quantity" = $1 AND "kobo_apis"."month_and_year" = $2 AND "kobo_apis"."lemur_category" = $3 AND "kobo_apis"."location_admin1" = $4 AND "kobo_apis"."location_admin2" = $5 ORDER BY "kobo_apis"."id" ASC LIMIT 1 [["lemurs_quantity", 5], ["month_and_year", "2016-01-01"], ["lemur_category", "brown_lemur"], ["location_admin1", "fianarantsoa"], ["location_admin2", "atsimo-atsinanana"]]
Upvotes: 0
Views: 451
Reputation: 828
Chalk this up to inexperience. Everything was working fine - it turns out I was viewing the response as displayed by my browser and not viewing the source. Details below.
As displayed in browser
{
kobo_data: [
{
lemurs_quantity: 1,
month_and_year: "2013-06-01",
lemur_category: "no_response",
location_admin1: "antsiranana",
location_admin2: "diana"
}, {
lemurs_quantity: 1,
month_and_year: "2013-06-01",
lemur_category: "no_response",
location_admin1: "antsiranana",
location_admin2: "diana"
},
]
}
vs. Source
{
"kobo_data": [
{
"lemurs_quantity": 1,
"month_and_year": "2013-06-01",
"lemur_category": "no_response",
"location_admin1": "antsiranana",
"location_admin2": "diana"
}, {
"lemurs_quantity": 1,
"month_and_year": "2013-06-01",
"lemur_category": "no_response",
"location_admin1": "antsiranana",
"location_admin2": "diana"
},
]
}
Upvotes: 0