# Syntax error when saving a template contain tojson

**URL:** <https://discuss.elastic.co/t/syntax-error-when-saving-a-template-contain-tojson/104950>\
**Category:** Elasticsearch\
**Created:** [October 23, 2017, 6:22pm UTC](https://discuss.elastic.co/t/syntax-error-when-saving-a-template-contain-tojson/104950 "2017-10-23T18:22:25Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![RomainXie](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/romainxie/32/22427_2.png) [@RomainXie](https://discuss.elastic.co/u/RomainXie)\
**Post date:** [October 23, 2017, 6:22pm UTC](https://discuss.elastic.co/t/syntax-error-when-saving-a-template-contain-tojson/104950/1 "2017-10-23T18:22:25Z")

</div>

Hi,

I attempt save a search template with a term query.

```
POST _scripts/test-json
{
  "script": {
    "lang": "mustache",
    "source": {"query": {"filter": {"bool": {"must": [{"terms": {"beat.hostname": {{#tojson}}hostname{{/tojson}} } }] } } } }
  }
}

```

But I got a syntax error without the double quote.

I saw the issue in github.  
[https://github.com/elastic/elasticsearch/issues/21648](https://github.com/elastic/elasticsearch/issues/21648)

I changed the template like the sample in the issue, it's no syntax error. But I can not get the result.

```
POST _scripts/test-json
{
  "script": {
    "lang": "mustache",
    "source": {"query": {"filter": {"bool": {"must": [{"terms": {"beat.hostname": ["{{hostname}}"] } }] } } } }
  }
}

```

And then I changed the template:

```
POST _scripts/test-json
{
  "script": {
    "lang": "mustache",
    "source": {"query": {"filter": {"bool": {"must": [{"terms": {"beat.hostname": ["{{#tojson}}hostname{{/tojson}}}}"] } }] } } } }
  }
}

```

After call is, I got a runtime error.

```
  {
    "type": "json_parse_exception",
    "reason": "Unexpected character ('h' (code 104)): was expecting comma to separate Array entries\n at [Source: org.elasticsearch.common.bytes.BytesReference$MarkSupportingStreamInputWrapper@396f6738; line: 1, column: 190]"
  }

```

How could I use the terms in a pre-registered template?

Thank you.

---

<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 20, 2017, 6:22pm UTC](https://discuss.elastic.co/t/syntax-error-when-saving-a-template-contain-tojson/104950/2 "2017-11-20T18:22:32Z")

</div>

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