Data Transformation : Create Dynamic field using ruby for array

Hi Team,

Hope you doing well!
I am facing problem in creating dynamic field for an array in logstash.
The list of array is below:
[{"usage_start_time": "2022-08-07T22:00:00Z",
"usage_end_time": "2022-08-07T23:00:00Z",
"labels": [{
"key": "env",
"value": "Prod"
}, {
"key": "nodeID",
"value": "abcd"

I need to create [Labels][key] as a new field and [Labels][value] as the field-value. Now, this array length can be different for different event, also the key is not fixed.
Expected output for this message:
"usage_start_time": "2022-08-07T22:00:00Z",
"usage_end_time": "2022-08-07T23:00:00Z",
"env": "prod",
Please help me here, Let me know if any details I need to provide.

See this thread.

Thanks @Badger , it was really easy :slight_smile:

ruby {
code => '
event.get("Labels").each do |item|
k = item["key"]
v = item["value"]

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