# Watcher pulling \_cat/indices and splitting results on newline to create multiple documents?

**URL:** https://discuss.elastic.co/t/watcher-pulling-cat-indices-and-splitting-results-on-newline-to-create-multiple-documents/351003
**Category:** Kibana
**Tags:** painless
**Created:** [January 13, 2024, 3:17am UTC](https://discuss.elastic.co/t/watcher-pulling-cat-indices-and-splitting-results-on-newline-to-create-multiple-documents/351003 "2024-01-13T03:17:18Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![dreynolds](https://avatars.discourse-cdn.com/v4/letter/d/9dc877/32.png) [@dreynolds](https://discuss.elastic.co/u/dreynolds)
#### Post date: [January 13, 2024, 3:17am UTC](https://discuss.elastic.co/t/watcher-pulling-cat-indices-and-splitting-results-on-newline-to-create-multiple-documents/351003/1 "2024-01-13T03:17:19Z")

</div>

I'm successfully pulling \_cat/indices with a watcher but can't get the results split based on each line.

The below works, but the output is all one document.

```auto
{
  "trigger": {
    "schedule": {
      "interval": "12h"
    }
  },
  "input": {
    "http": {
      "request": {
        "scheme": "https",
        "host": "es.myhost.com",
        "port": 9200,
        "method": "get",
        "path": "/_cat/indices",
        "params": {},
        "headers": {
          "Authorization": "ApiKey <api_key>"
        }
      }
    }
  },
  "condition": {
    "always": {}
  },
"actions": {
    "index_payload": {
      "transform": {
        "script": {
          "source": "ctx.payload",
          "lang": "painless"
        }
      },
      "index": {
        "index": "cat_indices_results"
      }
    }
  }
}

```

However, when I try to split the result into separate docs, splitting on new line it doesn't work.

Neither of these work:

```auto
"source": """return ['docs': ctx.payload._value.splitOnToken('\n')]""",

"source": """return ['docs': ctx.payload._value.splitOnToken((String)(char)0x0a)]""",

```

---

<div class="post-metadata">

### Author: ![yago82](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yago82/32/97755_2.png) [@yago82](https://discuss.elastic.co/u/yago82)
#### Post date: [January 13, 2024, 8:47am UTC](https://discuss.elastic.co/t/watcher-pulling-cat-indices-and-splitting-results-on-newline-to-create-multiple-documents/351003/2 "2024-01-13T08:47:04Z")

</div>

> [@dreynolds](#):
>
> However, when I try to split the result into separate docs, splitting on new line it doesn't work.
> 
> Neither of these work:
> 
> ```auto
> "source": """return ['docs': ctx.payload._value.splitOnToken('\n')]""",
> 
> "source": """return ['docs': ctx.payload._value.splitOnToken((String)(char)0x0a)]""",
> 
> ```

Hi,

you can use the \_cat/indices API with the format parameter set to json. This will return a JSON response that you can then process as needed.

```auto
...
        "path": "/_cat/indices",
        "params": {
          "format": "json"
...

```

Regards

---

<div class="post-metadata">

### Author: ![dreynolds](https://avatars.discourse-cdn.com/v4/letter/d/9dc877/32.png) [@dreynolds](https://discuss.elastic.co/u/dreynolds)
#### Post date: [January 22, 2024, 6:54pm UTC](https://discuss.elastic.co/t/watcher-pulling-cat-indices-and-splitting-results-on-newline-to-create-multiple-documents/351003/3 "2024-01-22T18:54:10Z")

</div>

Thank you, that pulls each line back as a json object, now I just need to split them into separate documents.

---

<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: [February 19, 2024, 6:55pm UTC](https://discuss.elastic.co/t/watcher-pulling-cat-indices-and-splitting-results-on-newline-to-create-multiple-documents/351003/4 "2024-02-19T18:55:04Z")

</div>

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