My array of objects has different keys, How can I chenged them to the same name?

Hello,
I have the following array:
{
"by_hour" : [
{
"hour06" : "06",
"nr_artigos06" : 0,
"caixa_aberta06" : 0,
"nr_taloes06" : 1,
"vendas_liquidas06" : 0,
"service_time06" : null
},
{
"nr_taloes10" : 6,
"service_time10" : 256.024,
"hour10" : "10",
"nr_artigos10" : 72,
"caixa_aberta10" : 1,
"vendas_liquidas10" : 105.67
},
{
"nr_taloes11" : 11,
"service_time11" : 246.67300000000003,
"caixa_aberta11" : 1,
"nr_artigos11" : 104,
"vendas_liquidas11" : 151.98,
"hour11" : "11"
}
]
}
And I need to have the same variable name for each position to make a split, like the following:
{
"by_hour" : [
}
"hour" : "06",
"nr_artigos" : 0,
"caixa_aberta" : 0,
"nr_taloes" : 1,
"vendas_liquidas" : 0,
"service_time" : null
},
{
"nr_taloes" : 6,
"service_time" : 256.024,
"hour" : "10",
"nr_artigos" : 72,
"caixa_aberta" : 1,
"vendas_liquidas" : 105.67
},
{
"nr_taloes" : 11,
"service_time" : 246.67300000000003,
"caixa_aberta" : 1,
"nr_artigos" : 104,
"vendas_liquidas" : 151.98,
"hour" : "11"
}
]
}
How can I make that happen?
Thank you

You would have to use a ruby filter if you want to change the field names. Something like

ruby {
    code => '
        byHour = event.get("by_hour")
        if byHour.is_a? Array
            byHour.each_index { |x|
                byHour[x].transform_keys! { |k| k.gsub(/\d+$/, "") }
            }
            event.set("by_hour", byHour)
        end
    '
}
1 Like

Thank you very much! It solved <3

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.