Adding an Id into an array field and split filter

Hello,

I have a event that I want to split in 2 because the field 'object.Voie' is a json array composed of 2 item.
When splitting the document I miss one information, I need to add an Id corresponding to the index of each item inside the array 0 and 1
Using the split filter I cannot add a new field with the value 0 and 1.

How can I add a new field with these value, using a ruby filter
ruby {
code => '
event.get("[object][Voie]").each_with_index do |hash, index|
event.set("[object][Voie][Id]", %{index})
end
'
}

The filter doesnot work with no error.

The original array in which I need to add:
"Id": 0
and
"Id": 1
in each item of the array. there are always 2 items only in the array

object.Voie	  {
  "Active": null,
  "Compteur": 121,
  "ControleFraude": null,
  "CompteurGaz": null,
  "Alarme": {
    "SeuilDeclanchementFuite": null,
    "Fraude": "",
    "SeuilDeclanchementDebit": null,
    "Fuite": "",
    "Surdebit": ""
  },
  "Debit": {
    "Max": null,
    "Min": null
  },
  "Delta": [],
  "FiltreAntiRebond": null,
  "NbrPeriodeSousSeuilFuite": null
},
{
  "Active": null,
  "Compteur": 20,
  "ControleFraude": null,
  "CompteurGaz": null,
  "Alarme": {
    "SeuilDeclanchementFuite": null,
    "Fraude": "",
    "SeuilDeclanchementDebit": null,
    "Fuite": "",
    "Surdebit": ""
  },
  "Debit": {
    "Max": null,
    "Min": null
  },
  "Delta": [],
  "FiltreAntiRebond": null,
  "NbrPeriodeSousSeuilFuite": null
}

If you have an object like

{ "object" : { "Voie": [
{ "Active": null, "Compteur": 121 },
{ "Active": null, "Compteur": 20 }
] } }

then you can add an id using

    ruby {
        code => '
            event.get("[object][Voie]").each_index { |index|
                event.set("[object][Voie][#{index}][Id]", index)
            }
        '
    }

before the split.

Thanks you!
So easy :slight_smile:

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