Reputation: 9216
I'm getting a strange error when trying to convert my object to json for an API connection. The following details my experience.
If I call
JSON.generate(self)
the output is
{"validation_context":null,"errors":{},"params":{"number":"123","name":"test"}}
I only need the params in my json object and when I call
JSON.generate(self.params) # or the next line
JSON.generate(@params) #params has been set on the object as an accessor
I get
undefined method `merge' for #<JSON::Ext::Generator::State:0x1043f1a38>
For some reason params
is not considered a Hash. It serializes ok when I'm getting the parent object but fails otherwise. How can I serialize just the params?
Upvotes: 2
Views: 1056
Reputation: 9216
Turns out I found a relatively simple solution.
Rather than
JSON.generate(object_to_serialize)
Using
object_to_serialize.to_json
Will work as intended.
Upvotes: 2