# Function toJson in stored search template

**URL:** <https://discuss.elastic.co/t/function-tojson-in-stored-search-template/201430>\
**Category:** Elasticsearch\
**Created:** [September 27, 2019, 3:34pm UTC](https://discuss.elastic.co/t/function-tojson-in-stored-search-template/201430 "2019-09-27T15:34:24Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![p\_tarte](https://avatars.discourse-cdn.com/v4/letter/p/ecc23a/32.png) [@p\_tarte](https://discuss.elastic.co/u/p_tarte)\
**Post date:** [September 27, 2019, 3:34pm UTC](https://discuss.elastic.co/t/function-tojson-in-stored-search-template/201430/1 "2019-09-27T15:34:24Z")

</div>

Hello,

I'm trying to save and call a search template having a parameter that is transformed using the toJson function. Here is an example:

POST \_scripts/testScript  
{  
"script": {  
"lang": "mustache",  
"source": "{ "query": { "terms" : { "status" : {{#toJson}}statuses{{/toJson}} }}}" }}

The render function is correctly generating the query:

GET \_render/template/testScript  
{  
"params": {  
"statuses" : ["pending", "published"]  
}  
}  
}

Output:  
{  
"template\_output" : {  
"query" : {  
"terms" : {  
"status" : [  
"pending",  
"published"  
]  
}  
}  
}  
}

However, when I call it to actually perform the search, I receive an error message:

GET \_search/template  
{  
"id": "testScript"  
"params": {  
"statuses" : ["pending", "published"]  
}  
}

Output:  
{  
"error": {  
"root\_cause": [  
{  
"type": "json\_parse\_exception",  
"reason": "Unexpected character ('"' (code 34)): was expecting comma to separate Object entries\n at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@43e766ba; line: 3, column: 4]"  
}  
],  
"type": "json\_parse\_exception",  
"reason": "Unexpected character ('"' (code 34)): was expecting comma to separate Object entries\n at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@43e766ba; line: 3, column: 4]"  
},  
"status": 500  
}

I tried different strategies : inline definition, not inline, getting the terms keyword inside the parameter, etc. I always end up having the same parsing error.

Anyone has an idea what I'm misssing here? I'm using ES 7.3.2.

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:** [October 25, 2019, 3:34pm UTC](https://discuss.elastic.co/t/function-tojson-in-stored-search-template/201430/2 "2019-10-25T15:34:53Z")

</div>

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