# Alerting: Access @timestamp in transform throws Exception

**URL:** <https://discuss.elastic.co/t/alerting-access-timestamp-in-transform-throws-exception/69677>\
**Category:** Elasticsearch\
**Created:** [December 21, 2016, 2:20pm UTC](https://discuss.elastic.co/t/alerting-access-timestamp-in-transform-throws-exception/69677 "2016-12-21T14:20:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nick.e](https://avatars.discourse-cdn.com/v4/letter/n/8dc957/32.png) [@nick.e](https://discuss.elastic.co/u/nick.e)\
**Post date:** [December 21, 2016, 2:20pm UTC](https://discuss.elastic.co/t/alerting-access-timestamp-in-transform-throws-exception/69677/1 "2016-12-21T14:20:19Z")

</div>

I am going to transform the @timestamp value of every hit in my watch and write it to another field.

When trying to access the @timestamp value with painless, I am receiving an error:

> ```
> "type": "general_script_exception",
> "reason": "failed to compile script [ScriptException[compile error]; nested: IllegalArgumentException[unexpected character [@].]; nested: LexerNoViableAltException;]"
> },
> "status": 500
> 
> ```

My transform-part looks as follows:

> ```
> "transform" : {
> "script" : "for (int i=0; i<ctx.payload.hits.hits.size(); i++)
> 
> ```

```
           { 
                ctx.payload.hits.hits[i].formatted_timestamp = ctx.payload.hits.hits[i]._source.@timestamp
           }
           return ctx.payload"           

```

> ```
> },
> 
> ```

Is there any trick to access the @timestamp-field?

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [December 21, 2016, 2:53pm UTC](https://discuss.elastic.co/t/alerting-access-timestamp-in-transform-throws-exception/69677/2 "2016-12-21T14:53:02Z")

</div>

Hey,

try using `ctx.payload.hits.hits[i]._source['@timestamp']`, like in this example

```auto
POST _xpack/watcher/watch/_execute
{
  "watch" :{
    "trigger" : {
      "schedule" : { "interval" : "1h" }
    },
    "input" : {
      "simple" : {
        "foo" : {
          "@timestamp" : "my timestamp"
        }
      }
    },
    "transform" : {
      "script" : {
        "inline" : "ctx.payload.foo['@timestamp'] = 'bar' ; return ctx.payload"
      }
    },
    "actions" : {
      "logging" : {
        "logging" : {
          "text" : "{{ctx.payload}}"
        }
      }
    }
  }
}

```

--Alex

---

<div class="post-metadata">

**Author:** ![nick.e](https://avatars.discourse-cdn.com/v4/letter/n/8dc957/32.png) [@nick.e](https://discuss.elastic.co/u/nick.e)\
**Post date:** [December 21, 2016, 3:09pm UTC](https://discuss.elastic.co/t/alerting-access-timestamp-in-transform-throws-exception/69677/3 "2016-12-21T15:09:20Z")

</div>

> [@spinscale](#):
>
> ctx.payload.hits.hits[i].\_source['@timestamp']

did exactly what i wanted, Thanks!

---

<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:** [January 18, 2017, 3:09pm UTC](https://discuss.elastic.co/t/alerting-access-timestamp-in-transform-throws-exception/69677/4 "2017-01-18T15:09:50Z")

</div>

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