# Access nested keys in Groovy

**URL:** <https://discuss.elastic.co/t/access-nested-keys-in-groovy/21568>\
**Category:** Elasticsearch\
**Created:** [January 10, 2015, 12:16am UTC](https://discuss.elastic.co/t/access-nested-keys-in-groovy/21568 "2015-01-10T00:16:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jeff\_Steinmetz](https://avatars.discourse-cdn.com/v4/letter/j/ecd19e/32.png) [@Jeff\_Steinmetz](https://discuss.elastic.co/u/Jeff_Steinmetz)\
**Post date:** [January 10, 2015, 12:16am UTC](https://discuss.elastic.co/t/access-nested-keys-in-groovy/21568/1 "2015-01-10T00:16:12Z")

</div>

Is there a way to easily check for the existence of a key and subsequently  
access keys in complex object (nested, maybe at times 3 levels deep  
unfortunately)  
simplified example doc:  
{  
"level1" : {  
"level2" : {  
"links" : ["[http://bit.ly/abc](http://bit.ly/abc)", "[http://bit.ly/abc](http://bit.ly/abc)", "[http://bit.ly/def](http://bit.ly/def)",  
"[http://bit.ly/ghi](http://bit.ly/ghi)"]  
}  
},  
"title" : "test123"  
}

This doesn't seem to work (form what I can tell it never finds the key  
level1.level2.links even when it does exist)

```
  "script" : "if (ctx._source.containsKey('level1.level2.links') ) 

```

{ctx.\_source.links\_url\_count = ctx.\_source['level1.level2.links''].size() }  
else { ctx.\_source.links\_url\_count = 0 }"

Simple keys work though like ctx.\_source.containsKey('title')

Does it require intermediate checks for every level (which could be a messy  
script)?

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/c3d0e9aa-ac68-4775-85d2-06a0bf3f3bd4%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/c3d0e9aa-ac68-4775-85d2-06a0bf3f3bd4%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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 6, 2017, 12:39am UTC](https://discuss.elastic.co/t/access-nested-keys-in-groovy/21568/2 "2017-07-06T00:39:46Z")

</div>

Closed on July 6, 2017, 12:39am UTC


