Converting a JSON array into object key pairs

What I'm trying to achieve is I have input like

 "jsonBody": {
         "measurements": [
           {
             "value": 0,
             "statistic": "VALUE"
           },
          {
             "value": 1,
             "statistic": "VALUE2"
           },
         ]
}

and I am trying to convert it into something like this

 "jsonBody": {

      "measurements"
        {
           "VALUE":0,
           "VALUE2":1
        }
}

So far i Have tried to do this

filter{
      ruby{
        code => 'b=  event.get("[response][jsonBody][measurements]")
        
        
        Array(b).each{|kv| event.set(kv["statistic"], kv["value"])}'
          
      }


}

and

filter{
      ruby{
        code => 'b=  event.get("[response][jsonBody][measurements]")
        
        
        b.each{|kv| event.set(kv["statistic"], kv["value"])}'
          
      }


}

Both of these methods don't seem to work very well.
What am I missing?

Try

ruby {
    code => '
        b = event.get("[response][jsonBody][measurements]")
        h = {}
        b.each { |kv|
            h[kv["statistic"]] = kv["value"]
        }
        event.set("[response][jsonBody][measurements]", h)
    '
  }

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