I am using Rails 4 and got following JSON output from different sources
source1
@something1 = book.find().to_json
output
"[{\"a\": \"val1\"}]"
source2
@something2 = Author.where(title: :autitle).pluck(:val2).to_json
Output
"[{\"b\": \"val2\"}]"
source 3
@something3 = Publications.find_by_pub_id(id)
Output
{
"c":"val3",
"d":" val4"
}
I want the final output like
{
"a": "val1",
"b": "val2",
"c":"val3",
"d":" val4"
}
I have used merge like
@newval= @something1[0].merge(@something2[0]).merge(@something3)
But, it gives error
undefined method merge!
Those variables are inside index method like
class Test controller < api::controller
def index
@something1 = ..
@something2 = ..
@something3 = ..
end
end
Hope it is clear.
@somethingvariables.Stringand thus the correct output for the first example is actually"[{\"a\":\"val1\"}]".Stringdoes not have amergemethod which would have been more obvious had the OP posted the full message. e.g.undefined method merge! for "[{\"a\":\"val1\"}]":Stringeditted question for correct outputto_jsoncalls from the first 2 and add it to the end of the merge chain. See Updated version of @Gerry's replto_json, so now it makes sense. Thank you for claryfing it.