[DONE] Using webhook payload in calculation

I’m tired so it could be I am being very daft! however, should I be able to use a payload variable in a calculation field? In this screenshot (if you ignore the slightly strange use case) Apple shortcuts has sent data to the webhook, however in the first calculation to get the hour I would like to use the webhook payload/time rather than using the current time. I can work around it in several ways including doing the calculation in a separate automation or even doing all the calculations in Shortcuts but I feel I am either missing something it it is intended to be easier.

1 Like

Hi @Jason,

thanks for sharing this use case and your question. :bulb:

Indeed, you have two ways of accessing the webhook payload inside calculations.

For both, you will need the webhook_payload_parsed variable that is available in all webhook receive trigger workflow automations. It will contain the full payload parsed as a JSON JavaScript object. Due to the dynamic nature of webhooks its structure varies based on the triggering POST request that was received.

Option #1

Your first option is to use that variable and access its properties directly. Your calculation script could then look as follows:

webhook_payload_parsed.time.split(':')[0]

Just be careful if your fields are nullable - so if .time would not be defined, this would throw an error at runtime. You can make it more defensive by for example performing a check using the ternary operator:

webhook_payload_parsed.time ? webhook_payload_parsed.time.split(':')[0] : null

Option #2

Working with JSON objects can be cumbersome, especially if you have arrays, need to perform aggregations etc. That is why we added JSONata as library, which enables the usage of the JSONata query language. It is a bit overkill for your use case, but you can also do this:

jsonata('time').evaluate(webhook_payload_parsed).split(':')[0]

For more complex access patterns and deeply nested JSON webhook payloads that can be useful though. Tape also uses JSONata under the hood to yield and access the suggested webhook variable tokens that you showed on top of your screenshot.

Let me know if that works and if it solves your issue. :rocket:

Cheers & happy building
Tim

Thanks, @Tim
So simple, and makes total sense.

Jason

1 Like

I don’t see a mention of it in the Libraries page: Available Libraries | Tape Developers

Does JSONNata also work in calculation fields?

1 Like

@Jason: Very happy to hear! :partying_face:

@shir thanks for the hint - it has just been added. Note the difference between calculation field libraries and automation libraries inside the docs. The environments are quite similar, but not exactly equal (due to the synchronous nature of calc fields).

Currently, jsonata is not available for calc fields. Feel free to a respective create feature request if it would add value to your use cases. Adding libraries is not a big thing usually due to our deployment and development agility.

Cheers
Tim

1 Like