# Add new field based on the result of array

**URL:** <https://discuss.elastic.co/t/add-new-field-based-on-the-result-of-array/110537>\
**Category:** Logstash\
**Created:** [December 6, 2017, 3:54pm UTC](https://discuss.elastic.co/t/add-new-field-based-on-the-result-of-array/110537 "2017-12-06T15:54:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![moriol](https://avatars.discourse-cdn.com/v4/letter/m/439d5e/32.png) [@moriol](https://discuss.elastic.co/u/moriol)\
**Post date:** [December 6, 2017, 3:54pm UTC](https://discuss.elastic.co/t/add-new-field-based-on-the-result-of-array/110537/1 "2017-12-06T15:54:26Z")

</div>

I have part of JSON appearing as below:  
{ "SampleName" =\> [  
[0] "name1",  
[1] "name2",  
[2] "name3",  
[3] "name4"  
],  
}  
I want to do the following:  
If the SampleName[0] is equal to name1, create a field called Value1.  
If the SampleName[1] is equal to name2, create a field called Value2.

I am doing the following, but no match. Can you please let me know where is my mistake?  
if "SampleName[0]"=~ "name1" {  
mutate {  
add\_field =\> { "Value1" =\> "test" }  
}  
}

---

<div class="post-metadata">

**Author:** ![moriol](https://avatars.discourse-cdn.com/v4/letter/m/439d5e/32.png) [@moriol](https://discuss.elastic.co/u/moriol)\
**Post date:** [December 6, 2017, 4:10pm UTC](https://discuss.elastic.co/t/add-new-field-based-on-the-result-of-array/110537/2 "2017-12-06T16:10:42Z")

</div>

Got it!  
It will be:  
if [SampleName][0] == "name1" {  
mutate {  
add\_field =\> { "Value1" =\> "test" }  
}  
}

---

<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:** [January 3, 2018, 4:11pm UTC](https://discuss.elastic.co/t/add-new-field-based-on-the-result-of-array/110537/3 "2018-01-03T16:11:06Z")

</div>

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