# Search templates {{#toJson}} with arrays: unexpected character

**URL:** <https://discuss.elastic.co/t/search-templates-tojson-with-arrays-unexpected-character/295398>\
**Category:** Elasticsearch\
**Created:** [January 25, 2022, 8:58pm UTC](https://discuss.elastic.co/t/search-templates-tojson-with-arrays-unexpected-character/295398 "2022-01-25T20:58:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tristan2](https://avatars.discourse-cdn.com/v4/letter/t/0ea827/32.png) [@Tristan2](https://discuss.elastic.co/u/Tristan2)\
**Post date:** [January 25, 2022, 8:58pm UTC](https://discuss.elastic.co/t/search-templates-tojson-with-arrays-unexpected-character/295398/1 "2022-01-25T20:58:29Z")

</div>

I'm trying to parameterize _\_source_ in a search template but I'm encountering an error.

This is my template:

```auto
PUT _scripts/stored-script
{
  "script": {
    "lang": "mustache",
    "source": {
      "_source": "{{#toJson}}fields{{/toJson}}"
    }
  }
}

```

```auto
GET myIndex/_search/template
{
  "id": "stored-script",
  "params": {
    "fields": ["test", "title"]
  }
}

```

I'm getting the following error:

```auto
{
  "error" : {
    "root_cause" : [
      {
        "type" : "json_parse_exception",
        "reason" : "Unexpected character ('t' (code 116)): was expecting comma to separate Object entries\n at [Source: (StringReader); line: 1, column: 16]"
      }
    ],
    "type" : "json_parse_exception",
    "reason" : "Unexpected character ('t' (code 116)): was expecting comma to separate Object entries\n at [Source: (StringReader); line: 1, column: 16]"
  },
  "status" : 400
}

```

According to this stackoverflow, this should work.

> <https://stackoverflow.com/questions/66581544/how-to-make-source-field-dynamic-in-elasticsearch-search-template>

Am I missing something?

---

<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 22, 2022, 8:58pm UTC](https://discuss.elastic.co/t/search-templates-tojson-with-arrays-unexpected-character/295398/2 "2022-02-22T20:58:43Z")

</div>

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