# Splitting each element(s) in nested array field into 2 respectively

**URL:** <https://discuss.elastic.co/t/splitting-each-element-s-in-nested-array-field-into-2-respectively/153515>\
**Category:** Logstash\
**Created:** [October 23, 2018, 4:19am UTC](https://discuss.elastic.co/t/splitting-each-element-s-in-nested-array-field-into-2-respectively/153515 "2018-10-23T04:19:22Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dx123](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@dx123](https://discuss.elastic.co/u/dx123)\
**Post date:** [October 23, 2018, 4:19am UTC](https://discuss.elastic.co/t/splitting-each-element-s-in-nested-array-field-into-2-respectively/153515/1 "2018-10-23T04:19:22Z")

</div>

I've found a few threads online discussing this but what my situation requires is more complex. Basically I have a array data field called target, where in most of the cases in my feeds it just consists of 1 element but the rest are nested ( \> 2 elements).

An example of its nested syntax: "target": ["A - B", "P - Q", "X - Y"]

What I would like to achieve is to split each element by the dash (-) and thereafter push each first half into a new field called "main" and and the second half into another new field called "sub".

Final desired output:  
The field "main" should consist of A, P and X and the field "sub" should consist of B, Q and Y.

My code right now that doesn't completely work:

```
	if [target] {
	mutate {
		split => {
			"target" => "-"
		}
	}

	mutate {
		add_field => {
			"main" => "%{[target][0]}"
			"sub" => "%{[target][1]}"	
		}
	}
}

```

Anyone knows how to accomplish this? I know ruby filters are definitely required as well but I do not know how to write them.

---

<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:** [November 20, 2018, 4:19am UTC](https://discuss.elastic.co/t/splitting-each-element-s-in-nested-array-field-into-2-respectively/153515/2 "2018-11-20T04:19:23Z")

</div>

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