# How to use painless loop to create a multidimensional array from values stored in 2 single arrays?

**URL:** <https://discuss.elastic.co/t/how-to-use-painless-loop-to-create-a-multidimensional-array-from-values-stored-in-2-single-arrays/227947>\
**Category:** Kibana\
**Created:** [April 14, 2020, 3:08pm UTC](https://discuss.elastic.co/t/how-to-use-painless-loop-to-create-a-multidimensional-array-from-values-stored-in-2-single-arrays/227947 "2020-04-14T15:08:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![fbracq](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/fbracq/32/65869_2.png) [@fbracq](https://discuss.elastic.co/u/fbracq)\
**Post date:** [April 14, 2020, 3:08pm UTC](https://discuss.elastic.co/t/how-to-use-painless-loop-to-create-a-multidimensional-array-from-values-stored-in-2-single-arrays/227947/1 "2020-04-14T15:08:38Z")

</div>

I'm having documents like this in kibana:

...  
"children\_relationship\_type": [  
"Blood",  
"Blood",  
"Adopted",  
"Adopted"  
],  
"children\_uuid": [  
"a385a319",  
"f27e7c95",  
"e01ebd42",  
"773622e4"  
],  
...

I want to create a painless scripted field from these 2 fields, with the following output:

"siblings": [  
{"id": "a385a319","type": "Blood"},  
{"id": "27e7c95","type": "Blood"},  
{"id": "e01ebd42","type": "Adopted"},  
{"id": "773622e4","type": "Adopted"}  
],  
I'm using this painless script:

```auto
def res="";
def i=0;
for(i=0; i < doc['sibling_uuid'].length;i++){
    res +='{"id": "'+doc['sibling_uuid'][i] + '","type": "' + doc['sibling_relationship_type'][i]+"},";
  }
  return res;

```

But this gives the following (wrong) output:

"siblings": [  
{"id": "773622e4","type": "Blood"},{"id": "a385a319","type": "Adopted"},{"id": "e01ebd42","type": ""},{"id": "f27e7c95","type": "}  
]

Can you help me?

Thanks. Frank

---

<div class="post-metadata">

**Author:** ![fbracq](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/fbracq/32/65869_2.png) [@fbracq](https://discuss.elastic.co/u/fbracq)\
**Post date:** [April 14, 2020, 5:09pm UTC](https://discuss.elastic.co/t/how-to-use-painless-loop-to-create-a-multidimensional-array-from-values-stored-in-2-single-arrays/227947/2 "2020-04-14T17:09:16Z")

</div>

There is an error field names: read "sibling\_relationship\_type" instead of "children\_relationship\_type" and "sibling\_uuid" instead of "children\_uuid"

---

<div class="post-metadata">

**Author:** ![mikecote](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mikecote/32/58549_2.png) [@mikecote](https://discuss.elastic.co/u/mikecote)\
**Post date:** [April 17, 2020, 12:49pm UTC](https://discuss.elastic.co/t/how-to-use-painless-loop-to-create-a-multidimensional-array-from-values-stored-in-2-single-arrays/227947/3 "2020-04-17T12:49:51Z")

</div>

Hi @fbracq,

Is there a reason the data is stored in two separate arrays? It might be better to have them stored as `siblings` and have special handing when doing lookups in `children_relationship_type` and `children_uuid`.

---

<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:** [May 15, 2020, 12:49pm UTC](https://discuss.elastic.co/t/how-to-use-painless-loop-to-create-a-multidimensional-array-from-values-stored-in-2-single-arrays/227947/4 "2020-05-15T12:49:52Z")

</div>

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