2

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?

1 Answer 1

2

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.

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

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.