Using toJson in big search template

I cannot figure out how I should use the toJson when prototyping templates in the kibana dev console. If I simply use it like others mustache functions like the following example Kibana simply classifies it as a "bad string" and won't execute it (assuming the '{{#' syntax doesn't sit well with it):

POST _render/template
{
  "source": {
    "query": {
      "field": {{#toJson}}param{{/toJson}}
    }
  },
  "params": {
    "param": ["val1","val2"]
  }
}

If I wrap it in quotes (like the following example) elasticsearch returns a "json_parse_exception" with the reason being "Unclosed object or array found":

POST _render/template
{
  "source": {
    "query": {
      "field": "{{#toJson}}param{{/toJson}}"
    }
  },
  "params": {
    "param": ["val1","val2"]
  }
}

The documentation specifies that "To ensure the request body is valid JSON, the source is written in the string format" as in the following example (which works), but besides the fact that this is un-intuitive and seems like an unintended side-effect of something else, it also results in a block which Kibana won't syntax check (making it harder to prototype more complex templates):

POST _render/template
{
  "source": """{
    "query": {
      "field": {{#toJson}}param{{/toJson}}
    }
  }""",
  "params": {
    "param": ["val1","val2"]
  }
}

Is there's something that I'm missing? Is there a way to write large templates with toJson functionality and keep the kibana syntax highlighting and syntax check?

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