Webhook JSON Mustache Question

Hi!

I try to send webhook Key/Value pairs only if the corresponding Key/Value pair exist in the EVENT I got. Works with a tiny problem. Here the Details:

The custom JSON I use in the Webhook Integration:

{
"Unit": "{{{Unit}}}",
"SeqNum": "{{{SeqNum}}}",
"TSSend": "{{{TSSend}}}",
"GoodCounter": "{{{GoodCounter}}}",
"TSGCnt": "{{{TSGCnt}}}",
"{{#RejectCounter}}RejectCounter{{/RejectCounter}}": "{{{RejectCounter}}}",
"{{#TSRCnt}}TSRCnt{{/TSRCnt}}": "{{{TSRCnt}}}",
"{{#OpState}}OpState{{/OpState}}": "{{{OpState}}}",
"{{#TSOpState}}TSOpState{{/TSOpState}}": "{{{TSOpState}}}"
}

If an EVENT with all Key/Value pairs triggers the webhook all looks great…

EVENT
{
"Unit":"Prod-001"
"SeqNum":"15"
"TSSend":"2024-06-10 16:02:06"
"GoodCounter":"4"
"TSGCnt":"2024-06-10 16:02:03"
"RejectCounter":"1"
"TSRCnt":"2024-06-10 16:02:06"
"OpState":"0"
"TSOpState":"2024-06-10 16:02:03"
"DevName":"PMT-000002"
}

WEBHOOK
{
  "Unit": "Prod-001",
  "SeqNum": "15",
  "TSSend": "2024-06-10 16:02:06",
  "GoodCounter": "4",
  "TSGCnt": "2024-06-10 16:02:03",
  "RejectCounter": "1",
  "TSRCnt": "2024-06-10 16:02:06",
  "OpState": "0",
  "TSOpState": "2024-06-10 16:02:03"
}

But if an EVENT triggers a webhook where a Key/Value pair was avoided an artefact appears => "": ""

EVENT
{
"Unit":"Prod-001"
"SeqNum":"10"
"TSSend":"2024-06-10 16:01:06"
"GoodCounter":"6"
"TSGCnt":"2024-06-10 16:01:06"
"DevName":"PMT-000002"
}

WEBHOOK
{
  "Unit": "Prod-001",
  "SeqNum": "10",
  "TSSend": "2024-06-10 16:01:06",
  "GoodCounter": "6",
  "TSGCnt": "2024-06-10 16:01:06",
  "": ""
}

Here another example…

EVENT
{
"Unit":"Prod-001"
"SeqNum":"1"
"TSSend":"2024-06-10 15:59:30"
"GoodCounter":"0"
"TSGCnt":"2024-06-10 15:59:30"
"OpState":"1"
"TSOpState":"2024-06-10 15:59:30"
"DevName":"PMT-000002"
}

WEBHOOK
{
  "Unit": "Prod-001",
  "SeqNum": "1",
  "TSSend": "2024-06-10 15:59:30",
  "GoodCounter": "0",
  "TSGCnt": "2024-06-10 15:59:30",
  "": "",
  "OpState": "1",
  "TSOpState": "2024-06-10 15:59:30"
}

Where is my fault?

Thanks for any help
Andreas

The fix works something like this:

{
"Unit": "{{{Unit}}}",
"SeqNum": "{{{SeqNum}}}",
"TSSend": "{{{TSSend}}}",
"GoodCounter": "{{{GoodCounter}}}",
{{#RejectCounter}}"RejectCounter": "{{{RejectCounter}}}",{{/RejectCounter}}
{{#TSRCnt}}"TSRCnt": "{{{TSRCnt}}}",{{/TSRCnt}}
{{#OpState}}"OpState": "{{{OpState}}}",{{/OpState}}
{{#TSOpState}}"TSOpState": "{{{TSOpState}}}",{{/TSOpState}}
"TSGCnt": "{{{TSGCnt}}}"
}

What you need to do is surround the components that may or may not exist entirely with the conditional, including the separators.

I also moved TSGCnt to the end, because otherwise you could end up with invalid JSON if the TSOpState did not exist.

2 Likes

Hi rickkas7, that works :slight_smile: Thx so much!

One info the WebGui shows an parse error but I just saved the new code which is possible and does the job...

Andreas

Sorry, I forgot to warn you about that. Yes, it's flagged as an error but you can still save it and it will work properly.

1 Like

ChatGPT 4o suggests this style of code but I did not trust because of the parse error. You are more trustfully :slight_smile: I think I should change my beaviour...