Flattening array and dynamically create the field name

Hello,

I have an JSON array input below:

"coverageStats": [
                {
                    "label": "Branches",
                    "position": 6,
                    "total": 30,
                    "covered": 17,
                    "isDeltaAvailable": false,
                    "delta": 0.0
                },
                {
                    "label": "Lines",
                    "position": 4,
                    "total": 492,
                    "covered": 117,
                    "isDeltaAvailable": false,
                    "delta": 0.0
                }
            ]

and I would like to create something like this with a ruby filter:

Desired output:

 "Branches" : {
                    "label": "Branches"
                    "position": 6,
                    "total": 30,
                    "covered": 17,
                    "isDeltaAvailable": false,
                    "delta": 0.0
},
"Lines" : {
                   "label": "Lines",
                    "position": 4,
                    "total": 492,
                    "covered": 117,
                    "isDeltaAvailable": false,
                    "delta": 0.0
}

Basically, I would like to iterate the array and take the "label" value and set it as a key in my new structured dictionary. By doing this, it would make easier to display the event in Kibana.

Any help would be appreciated.
Thank you.

You could do that using ruby. I haven't tested it, but I would start with something like

ruby {
    code => '
        event.get("coverageStats").each { |v|
            event.set(v["label"], v)
        }
    '
}

Works like a charm!

Thank you.

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