Mustache Template function / Removing trailing comma?

What do we do when dealing with JSON object on webhooks? Current templating with aggregations basically makes it impossible due to trailing commas.

For example, using any kind of templating requires iterations over an object in the ES response object. However, every json iteration that will be seperated by a comma will always have a trailing comma like so:

{
  "obj1": "val1",
  "obj2": "val2",
  "obj3": "val3",
  "obj4": "val4", <- Sadface
}

The vast majority of JSON compliant webhooks will reject this / fail to decode the JSON object on the other end.

Any suggestions on how to get around this?

Hi @Dandy,

Could you give an example of where you’re getting the trailing comma from?

Thanks

Hello @dbbaughe

Sorry for the delay, but take any instance example where you’re iterating over an object. Lets say we have a webhook that is triggered when events are received. The webhook, as most do, expects JSON. Our ES response are aggrgated like so:

"aggs": { "2": { "keyword": "xyz", "aggs": [{ "keyword": "123" }, { "keyword": "456" }] }}  

Well, you could iterate over these results in Mustache pretty easily, but this has an inherent flaw in building a JSON object.

{
  "device": {
     "name": "xyz",
     "properties": ["123", "456",] <----- 
  } 

Example here is invalid JSON and most recipient webhooks won’t accept it because of the trailing comma. You can build the object any way you want, but no matter what, if you’re iterating over anything in the ES response object, you’ll end up with a trailing comma.

This is an inherent flaw in Mustache, not ES, but because webhooks traditionally expect JSON, normally to counter this you’d have a Mustache function, or JS to strip out the trailing comma, which we don’t have the option of doing here.

@dbbaughe Any ideas on this or an approach we could take to resolving this? I’m surprised more people who work with JSON via webhooks or the like aren’t experiencing a similar issue.