# Adding an Id into an array field and split filter

**URL:** <https://discuss.elastic.co/t/adding-an-id-into-an-array-field-and-split-filter/196223>\
**Category:** Logstash\
**Created:** [August 22, 2019, 3:23am UTC](https://discuss.elastic.co/t/adding-an-id-into-an-array-field-and-split-filter/196223 "2019-08-22T03:23:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![manunc](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/manunc/32/48340_2.png) [@manunc](https://discuss.elastic.co/u/manunc)\
**Post date:** [August 22, 2019, 3:23am UTC](https://discuss.elastic.co/t/adding-an-id-into-an-array-field-and-split-filter/196223/1 "2019-08-22T03:23:23Z")

</div>

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
}
```

---

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [August 22, 2019, 12:41pm UTC](https://discuss.elastic.co/t/adding-an-id-into-an-array-field-and-split-filter/196223/2 "2019-08-22T12:41:22Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![manunc](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/manunc/32/48340_2.png) [@manunc](https://discuss.elastic.co/u/manunc)\
**Post date:** [August 22, 2019, 9:40pm UTC](https://discuss.elastic.co/t/adding-an-id-into-an-array-field-and-split-filter/196223/3 "2019-08-22T21:40:41Z")

</div>

Thanks you!  
So easy 🙂

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [September 19, 2019, 9:40pm UTC](https://discuss.elastic.co/t/adding-an-id-into-an-array-field-and-split-filter/196223/4 "2019-09-19T21:40:47Z")

</div>

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