1

I'm trying to send a JSON API response from my rails app to Dasheroo which expects the following format:

{
  my_statistic: { type: 'integer', value: 1, label: 'My Statistic' }
}

However it is not happy with my data structure generated by the following code:

In controller:

def count_foo_members
    @foo = Foo.all.count
end

In count_foo_members.json.jbuilder:

json.foo_members do
    json.type  'integer'
    json.value @foo
    json.label 'Foo Members'
end

If I open this route in my browser I can see the following:

{
    "foo_members":{"type":"integer","value":1,"label":"Foo Members"}
}

From the results above, the only thing that I can see that could have an effect on the result is the fact that my JSON result has quotation marks around the JSON Key values.

My question thus is: How can I remove these quotation marks in Rails 4 and JBuilder?

HermannHH
  • 1,732
  • 1
  • 27
  • 57
  • Go through this http://stackoverflow.com/questions/23388635/remove-quotes-from-jbuilder-json, hope it will help you – Gaurav Gupta Dec 08 '15 at 06:01
  • 2
    That is how JSON works, JSON has only string, integers and null values. You can't remove quotes, otherwise it's will be not JSON – Ivan Shamatov Dec 08 '15 at 06:59

2 Answers2

0

JSON.parse(you_response) and you get standart hash.

0

You cannot remove the quotation marks from the keys. The responsibility is on the consumer (Dasheroo) to parse your JSON string into a JavaScript Object, which will "remove" the quotes from the keys.

Read json-object-with-or-without-quotes for further practical insight.

Community
  • 1
  • 1
sealocal
  • 10,897
  • 3
  • 37
  • 50