# Find the value of a dynamic key inside an object

**URL:** https://discuss.elastic.co/t/find-the-value-of-a-dynamic-key-inside-an-object/126607
**Category:** Elasticsearch
**Created:** [April 3, 2018, 4:33pm UTC](https://discuss.elastic.co/t/find-the-value-of-a-dynamic-key-inside-an-object/126607 "2018-04-03T16:33:32Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![pungent](https://avatars.discourse-cdn.com/v4/letter/p/a698b9/32.png) [@pungent](https://discuss.elastic.co/u/pungent)
#### Post date: [April 3, 2018, 4:33pm UTC](https://discuss.elastic.co/t/find-the-value-of-a-dynamic-key-inside-an-object/126607/1 "2018-04-03T16:33:33Z")

</div>

My partial mapping is as follows:

```auto
    "name": {
      "type": "string"
    },
    "misc": {
      "type": "object",
      "dynamic": "true",
      "properties": {}
    }

```

Let's assume there is only one document in my storage, as follows

```auto
    {
      "name": "foobar",
      "misc": {
        "201804": 1,
        "201805": 10,
        "201806": 2,
        "201807": 1,
        "201808": 9,
        "201809": 1,
        "201810": 8,
        "201811": 1,
        "201812": 11
      }
    }

```

In my search query, I have groovy script, where I am trying to find the value of the key inside `misc` object

```auto
    dynamic_key = ....my logical calculation to find key....
    //assume the calculated value of dynamic_key is "201808"
    kvalue = doc['misc'][dynamic_key].value //value of kvalue should be 9 as dynamic_key="201808"

```

But I keep getting error

```auto
type: "no_class_def_found_error",
reason: "java/lang/Throwable"

```

for this line `kvalue = doc['misc'][dynamic_key].value` . But for the experiment purpose, if I write `kvalue = doc['misc.201805'].value` then it works fine. Any idea how to achieve what I am looking for? I am using groovy script.

---

<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 1, 2018, 4:34pm UTC](https://discuss.elastic.co/t/find-the-value-of-a-dynamic-key-inside-an-object/126607/2 "2018-05-01T16:34:12Z")

</div>

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