ruby on rails - Jbuilder - Array to JSON -
i have array named sales of following format --
2015 january 2403 2015 february 2420 2015 march 2320 2015 april 2230
array structure - [[2015, "january", 2403], [2015, "february", 2420]]
i'm looking construct following json response jbuilder
{ months : ['january', 'february', 'march', 'april'] sales : [2403, 2420, 2320, 2230] }
things i've tried --
i mapped months (and sales) array --
@months = @sales.map {|year, month, sales| [month]}
and in jbuilder --
{"months":[["february"],["january"],["december"],["november"]]}
which isn't want , neither efficient
some nice.
in action:
array = [[2015, "january", 2403], [2015, "february", 2420]] @data = array.transpose
in .json.jbuilder template:
json.months @data[1] json.sales @data[2]
Comments
Post a Comment