Hashtag Helpers
JSONata Hashtag Helper
Returns the result of a JSONata expression
Allows using JSONata to do much more complex Variable Substitutions in line without the need to use a separate task to perform what is required.
Syntax
~~#jsonata#( {JSONata Expression} )
Basic Usage
You can use this helper on the Value side (Right hand side) where you are providing some data. So anywhere where you would use Value replacement substitution, you can use this helper to do more complex data substitutions.
Example definition:
Substituted definition
Examples
Converting a JSON Structure to String
This is how you would use the JSONata Helper to convert JSON Data into a String for use where this is required.
You should now use the To JSON String helper to achieve this. It can be done inline and mixed with other Handlebars helpers / text.
We will use the jsonata expression $string() to achieve this.
Consider Data
Usage Example
Result
Try it in the JSONata Exerciser
Converting a JSON String back to JSON Structure
You should now use the Parse JSON Hashtag Helper to achieve this. It is a more direct and easier way to convert a JSON string back to a JSON structure.
This is how you would use the JSONata Helper to convert a String containing JSON into JSON Structured data for use where this is required.
We will use the jsonata expression $eval() to achieve this.
Consider Data
Usage Example
Result
Try it in the JSONata Exerciser
Clean Phone Number to E164
This is how you would use the JSONata Helper to clean a phone number to E164 International Format.
You can also use the JSONata SaveData task to perform this function.
Try it in the JSONata Exerciser
Consider Data in stub.data
These data points can easily be modified to point to the data points in your specific schema.
default_country_code
: The country Code to correct the number with. Defaults to '1'
- USA
if not found or given.
cellphone_number
: The phone number that needs to be cleaned and corrected
Usage Example
Try it in the JSONata Exerciser
stub.data.default_country_code
ONLY used if the cellphone number starts with a '0'
REPLACE the stub.data.default_country_code
with the static country code as string eg. '1' or with the path to the stub data country code data point.
If the 'default_country_code' does not exist or is not given, it will default to '1'
- USA
country code.
stub.data.cellphone_number
REPLACE the stub.data.cellphone_number
with the path to the stub data cellphone_number data point.
Result
Setting data depending on Stub Branch
This is how you would use the JSONata Helper to set the contents of a variable / field depending on the branch of the stub, draft
or live
.
This is particularly useful when draft
stubs should send api calls to a development environment and live
stubs should send api calls to a production environment, without the need for continual manual intervention.
Try it in the JSONata Exerciser
Consider Data
These data points are typical for a stub. The branch
is what we will be referring to in the JSONata expression.
templateuuid
: The templates unique uuid the stub subscribes to.
branch
: The branch of the templates the stub is using.
context
: The context of the template the stub is using.
Usage Example
Try it in the JSONata Exerciser
Result