# Watcher, Convert timestap field in loop

**URL:** <https://discuss.elastic.co/t/watcher-convert-timestap-field-in-loop/316428>\
**Category:** Elasticsearch\
**Tags:** elastic-stack-alerting, painless\
**Created:** [October 12, 2022, 10:01am UTC](https://discuss.elastic.co/t/watcher-convert-timestap-field-in-loop/316428 "2022-10-12T10:01:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ddoroshenko](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@ddoroshenko](https://discuss.elastic.co/u/ddoroshenko)\
**Post date:** [October 12, 2022, 10:01am UTC](https://discuss.elastic.co/t/watcher-convert-timestap-field-in-loop/316428/1 "2022-10-12T10:01:43Z")

</div>

Hi everyone,

I' d like to iterate through `ctx.payload.hits.hits` and convert `@timestamp` field from ISO8601 to milliseconds.

I have the following configuration:

```auto
"actions": {
  "log_hits": {
    "foreach": "ctx.payload.hits.hits",
    "max_iterations": 10,
    "transform": {
      "script": {
        "source": """
          String timestamp = ctx.payload['_source']['@timestamp'];
          ZonedDateTime datetime = ZonedDateTime.parse(timestamp);
          long milliSinceEpoch = datetime.toInstant().toEpochMilli();
          return ['epochtime':milliSinceEpoch];
        """,
        "lang": "painless"
      }
    },
    "logging": {
      "text": "{{ctx.payload.epochtime}}"
    }
  }

```

But in result I get an error

```auto
              "type" : "script_exception",
              "reason" : "runtime error",
              "script_stack" : [
                """timestamp = ctx.payload['_source']['@timestamp'];
          ZonedDateTime """,
                " ^---- HERE"
              ],
              "script" : " ...",
              "lang" : "painless",
              "position" : {
                "offset" : 41,
                "start" : 18,
                "end" : 92
              },
              "caused_by" : {
                "type" : "null_pointer_exception",
                "reason" : "cannot access method/field [normalizeIndex] from a null def reference"
              }
            }
          },
          "reason" : "Failed to transform payload"

```

Suppose I have a mistake somewhere in my loop.

---

<div class="post-metadata">

**Author:** ![stu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stu/32/75063_2.png) [@stu](https://discuss.elastic.co/u/stu)\
**Post date:** [October 12, 2022, 4:30pm UTC](https://discuss.elastic.co/t/watcher-convert-timestap-field-in-loop/316428/2 "2022-10-12T16:30:05Z")

</div>

From the error, it appears `ctx.payload` is null, which shouldn't be the case in the watcher transform context.

The `normalizeIndex` part of the message is code that tries to handle indices on def types that hold on array.

The transform can check for `ctx.payload == null` or, if you can narrow down the troublesome result, use `Debug.explain(ctx.payload)` to dump the contents of `ctx.payload`.

---

<div class="post-metadata">

**Author:** ![ddoroshenko](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@ddoroshenko](https://discuss.elastic.co/u/ddoroshenko)\
**Post date:** [October 13, 2022, 8:36am UTC](https://discuss.elastic.co/t/watcher-convert-timestap-field-in-loop/316428/3 "2022-10-13T08:36:47Z")

</div>

well, as I supposed, the problem was in incorrectly placed `transform` unit

this works

```auto
"transform": {
    "script": """
      def n = [];
      def hits = ctx.payload.hits.hits;
      for (hit in hits) {
        hit.epochtime = ZonedDateTime.parse(hit['_source']['@timestamp']).toInstant().toEpochMilli();
        n.add(hit);
      }
      return ['new':n]
    """
  },
  "actions": {
    "log_hits": {
      "foreach": "ctx.payload.new",
      "max_iterations": 10,
      "logging": {
        "text": "{{ctx.payload.epochtime}}"
      }
    }
  }

```

---

<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:** [November 10, 2022, 8:37am UTC](https://discuss.elastic.co/t/watcher-convert-timestap-field-in-loop/316428/4 "2022-11-10T08:37:19Z")

</div>

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