# Scripted Field in Kibana to extract from a nested document structure

**URL:** <https://discuss.elastic.co/t/scripted-field-in-kibana-to-extract-from-a-nested-document-structure/237380>\
**Category:** Kibana\
**Created:** [June 17, 2020, 1:50am UTC](https://discuss.elastic.co/t/scripted-field-in-kibana-to-extract-from-a-nested-document-structure/237380 "2020-06-17T01:50:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sanjaysubramanian](https://avatars.discourse-cdn.com/v4/letter/s/bcef8e/32.png) [@sanjaysubramanian](https://discuss.elastic.co/u/sanjaysubramanian)\
**Post date:** [June 17, 2020, 1:50am UTC](https://discuss.elastic.co/t/scripted-field-in-kibana-to-extract-from-a-nested-document-structure/237380/1 "2020-06-17T01:50:01Z")

</div>

Hi all

Kibana and ES 7.7.0

My doc structure is like this:

{  
"key1": "value1",  
"key2": "value3",  
"key3": {  
"key4": {  
"key5": "value5",  
"key6": "val6/val7/val8/val9.html"  
},  
"key7": "val10"  
},  
"key8": "val11",  
"key9": "val12"  
}

I need to add a scripted\_field in my index pattern that basically gives me **"val9"**

If I were to write the logic in python  
my\_val = doc['key3']['key4']['key6'][::-1].split('/')[0][::-1].split('.')[0]  
return my\_val

How do I do this in Painless ?

Thanks  
warmly

sanjay

---

<div class="post-metadata">

**Author:** ![sanjaysubramanian](https://avatars.discourse-cdn.com/v4/letter/s/bcef8e/32.png) [@sanjaysubramanian](https://discuss.elastic.co/u/sanjaysubramanian)\
**Post date:** [June 17, 2020, 3:30pm UTC](https://discuss.elastic.co/t/scripted-field-in-kibana-to-extract-from-a-nested-document-structure/237380/2 "2020-06-17T15:30:43Z")

</div>

I figured out how to do this. Sharing my solution with all.  
Thanks  
warmly  
sanjay

```auto
String htmlStr = doc['key3.key4.key6'].value;
StringBuffer sb1 = new StringBuffer(htmlStr);
sb1.reverse();
String htmlStr2 = /\//.split(sb1.toString())[0];
StringBuffer sb2 = new StringBuffer(htmlStr2);
sb2.reverse();
String htmlStr3 = /\./.split(sb2.toString())[0];
return htmlStr3;

```

---

<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:** [July 15, 2020, 3:30pm UTC](https://discuss.elastic.co/t/scripted-field-in-kibana-to-extract-from-a-nested-document-structure/237380/3 "2020-07-15T15:30:43Z")

</div>

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