# Generate an array of fields from other fields in an Array of Hashes

**URL:** <https://discuss.elastic.co/t/generate-an-array-of-fields-from-other-fields-in-an-array-of-hashes/94004>\
**Category:** Logstash\
**Created:** [July 20, 2017, 6:43pm UTC](https://discuss.elastic.co/t/generate-an-array-of-fields-from-other-fields-in-an-array-of-hashes/94004 "2017-07-20T18:43:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![corywink](https://avatars.discourse-cdn.com/v4/letter/c/6bbea6/32.png) [@corywink](https://discuss.elastic.co/u/corywink)\
**Post date:** [July 20, 2017, 6:43pm UTC](https://discuss.elastic.co/t/generate-an-array-of-fields-from-other-fields-in-an-array-of-hashes/94004/1 "2017-07-20T18:43:58Z")

</div>

Hello, I am trying to grab fields out of an array of hashes in Logstash. Right now it looks like this:

"hashes" =\> [  
&nbsp;&nbsp; [0] {  
&nbsp;&nbsp;&nbsp;&nbsp; "key1" =\> "value1",  
&nbsp;&nbsp;&nbsp;&nbsp; "key2"=\> "value2",  
&nbsp;&nbsp;&nbsp;&nbsp; "key3" =\> "value3",  
&nbsp;&nbsp; }  
&nbsp;&nbsp; [1] {  
&nbsp;&nbsp;&nbsp;&nbsp; "key1" =\> "value1",  
&nbsp;&nbsp;&nbsp;&nbsp; "key2"=\> "value2",  
&nbsp;&nbsp;&nbsp;&nbsp; "key3" =\> "value3",  
&nbsp;&nbsp; }  
&nbsp;&nbsp; [2] {  
&nbsp;&nbsp;&nbsp;&nbsp; "key1" =\> "value1",  
&nbsp;&nbsp;&nbsp;&nbsp; "key2"=\> "value2",  
&nbsp;&nbsp;&nbsp;&nbsp; "key3" =\> "value3",  
&nbsp;&nbsp; }  
]

I want to create an array with the fields of all the key2 values from each hash in the array. What would be the best way of going about this?

Right now I am using the ruby filter as such:

ruby {  
&nbsp;&nbsp; code =\> '  
&nbsp;&nbsp;&nbsp;&nbsp; hashes = event.get("hashes")  
&nbsp;&nbsp;&nbsp;&nbsp; vals= Array.new  
&nbsp;&nbsp;&nbsp;&nbsp; hashes.each {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |c| vals.push("#{c[key2]}")  
&nbsp;&nbsp;&nbsp;&nbsp; }  
&nbsp;&nbsp;&nbsp;&nbsp; event.set("vals",vals)  
&nbsp;&nbsp; '  
&nbsp;&nbsp; add\_tag =\> ["foo"]  
}

However, this does not seem to work. Also in the output, there is a \_rubyexception tag. So my ruby is wrong but I am not sure where. Am I going about this the right way? And what is wrong with my ruby?

Thanks

---

<div class="post-metadata">

**Author:** ![corywink](https://avatars.discourse-cdn.com/v4/letter/c/6bbea6/32.png) [@corywink](https://discuss.elastic.co/u/corywink)\
**Post date:** [July 20, 2017, 9:02pm UTC](https://discuss.elastic.co/t/generate-an-array-of-fields-from-other-fields-in-an-array-of-hashes/94004/2 "2017-07-20T21:02:07Z")

</div>

Works now. just needed to add quotes around "key2". That line now looks like this:

|c| vals.push(c["key2"])

---

<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:** [August 17, 2017, 9:02pm UTC](https://discuss.elastic.co/t/generate-an-array-of-fields-from-other-fields-in-an-array-of-hashes/94004/3 "2017-08-17T21:02:15Z")

</div>

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