1. Hashtag Helpers
  2. JSONata

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:

loading...

Substituted definition

loading...

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.

TIP

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.

NOTE

Consider Data

loading...

Usage Example

loading...

Result

loading...

Try it in the JSONata Exerciser

Converting a JSON String back to JSON Structure

TIP

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.

NOTE

Consider Data

loading...

Usage Example

loading...

Result

loading...

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

NOTE

Consider Data in stub.data
These data points can easily be modified to point to the data points in your specific schema.

loading...

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

editor
        {
    "tasktype": "savedata",
    "params": {
        "fieldname": "phonenumber",
        "savevalue": "~~#jsonata#($default_country_code := $string(stub.data.default_country_code); $number := $string(stub.data.cellphone_number) ~> $trim();  $default_country_code := $exists($default_country_code) ? $default_country_code : "1";  $result := $substring($number,0,1) = "0" ? ($cleaned_cellphone_number := $match($number, /[0-9]+/).match ~> $join(); $join(['+',$substring($cleaned_cellphone_number,0,$length($default_country_code)) = $default_country_code ? "" : $default_country_code,$substring($cleaned_cellphone_number,($substring($cleaned_cellphone_number,0,1) = "0" ? 1 : 0))])) : $join(['+',$cleaned_cellphone_number := $match($number, /[0-9]+/).match ~> $join()]))"
    }
}

      

Try it in the JSONata Exerciser

stub.data.default_country_code

optional
string

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

optional
string

REPLACE the stub.data.cellphone_number with the path to the stub data cellphone_number data point.


Result

loading...

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

NOTE

Consider Data
These data points are typical for a stub. The branch is what we will be referring to in the JSONata expression.

loading...

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

loading...

Try it in the JSONata Exerciser

Result

loading...