I am generating a hash out of three arrays and next trying to construct a json. I succeeded by json object has array.
require 'json'
A = [['A1', 'A2', 'A3'], [ "A1_xfactor", "A2_xfactor", "A3_xfactor"], ["A1_pull", "A2_pull", "A3_pull"]]
kl = { 'obj' => A[0].zip(A[1],A[2]).map { |k, v,l| { 'f1' => k, 'f2' => v, 'xfactor' => l} }}.to_json
puts kl
Output
{
"obj": [{
"f1": "A1",
"f2": "A1_xfactor",
"xfactor": "A1_pull"
}, {
"f1": "A2",
"f2": "A2_xfactor",
"xfactor": "A2_pull"
}, {
"f1": "A3",
"f2": "A3_xfactor",
"xfactor": "A3_pull"
}]
}
Instead of array, I would like have it without an array and F1 has to come out as a separate object.
{
"obj": {
"A1": {
"f2": "A1_xfactor",
"xfactor": "A1_pull"
},
"A2": {
"f2": "A2_xfactor",
"xfactor": "A2_pull"
},
"A3": {
"f2": "A3_xfactor",
"xfactor": "A3_pull"
}
}
}