Skip to main content

Using the JSONPath functionality

The Altcraft platform supports the functionality of the JSONPath query language when working with JSON content. JSONPath syntax allows you to quickly and easily select objects within code and use them message templates. It can be used when working with API content and with external JSON content.

API content allows you to change the content of the trigger message each time it is sent. The required variable can be specified in the trigger API request. You can learn more about how to run triggered campaigns and how to work with API content in messages here.

To use external JSON content, go to the Options section when creating or editing a template and provide a link to the dynamic JSON content source. The platform will automatically determine the fields of the external JSON file. More information about using JSON when working with message templates can be found here.

The JSONPath variable on the Altcraft platform is specified as follows:

{jsonpath(json.<root_object> "<path_to_object>" "<output_format>")}

Function arguments

ArgumentDescription
Root objectAccessing a JSON object that contains the desired parameter or object
Path to objectThe path to the desired object can consist of segments separated by dots, logical operators, filters or functions
Output formatThe final variable can be represented in two versions: item or array. Item will display the first result found for the request, and if there is no result, it will return an empty string. Array returns an array of all results found. This mode always outputs the result as an array, even if there is only one or no result.

Usage examples

Let's look at examples of using JSONPath. Suppose we have a JSON file with the following content:


{
"store": {
"book": [
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95,
"images": ["AbeLincoln.png","Churchill.jpg","Thatcher.png"]
},
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99,
"images": ["MenAtArms.png","Gentlemen.jpg","Officers.png"]
},
{
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99,
"images": ["Ahab.png","Ishmael.jpg","whale.png"]

},
{
"category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
]
}
}

With JSONPath functionality, a small expression can return an array containing all the authors of the books:

{jsonpath(json.store "$.book[*].author" "array")}

Another expression will return one object, the author of the first book:

{jsonpath(json.store "$.book[*].author" "item")}

You can build a more complex function, for example, in order to find out the name of a book whose price is higher than 10, we will compose the following code:

{jsonpath(json.store "$.book[?(@.price > 10)].title" "item")}

To find out the arithmetic average of the price of all books, we can use this function:

{jsonpath(json.store "$.book[*].price.avg()" "item")}

JSONPath variables can also be run in loops:


{for $index $imgs = jsonpath(json.store "$.book[*].images" "array")}
<li>{$imgs}</li>
{else}
<p>Empty array!</p>
{end}

The full functionality of JSONPath on the Altcraft Marketing platform is listed in the table below:

ArgumentDescriptionUse exampleResult
$Root element{jsonpath(json.store "$.book[0].title" "item")}Sayings of the Century
@Current element{jsonpath(json.store "$.book[?(@.price > 10)].title" "item")}Sword of Honour
..Recursive descent{jsonpath(json.store "$..title" "array")}[Sayings of the Century Sword of Honour Moby Dick The Lord of the Rings]
*Match all objects{jsonPath(json.store "$.book[*].price" "array")}[8.95 12.99 8.99 22.99]
?()Filter{jsonpath(json.store "$.book[?(@.category == 'reference')].title" "item")}Sayings of the Century
Boolean operator for filteringDescriptionUse exampleResult
==Equal to{jsonpath(json.store "$.book[?(@.category == 'reference')].title" "item")}Sayings of the Century
!=Not equal to{jsonpath(json.store "$.book[?(@.category != 'reference')].title" "array")}[Sword of Honour Moby Dick The Lord of the Rings]
<Less than{jsonpath(json.store "$.book[?(@.price < 10)].title" "array")}[Sayings of the Century Moby Dick]
>Greater than{jsonpath(json.store "$.book[?(@.price > 10)].title" "array")}[Sword of Honour The Lord of the Rings]
<=Less than or equal to{jsonpath(json.store "$.book[?(@.price >= 12.99)].title" "array")}[Sword of Honour The Lord of the Rings]
>=Greater than or equal to{jsonpath(json.store "$.book[?(@.price <= 12.99)].title" "array")}[Sayings of the Century Sword of Honour Moby Dick]
FunctionDescriptionUse exampleResult
maxMaximum value in array{jsonPath(json.store "$.book[*].price.max()" "item")}22.99
minMinimum value in array{jsonPath(json.store "$.book[*].price.min()" "item")}8.95
firstFirst array element{jsonPath(json.store "$.book[*].title.first()" "item")}Sayings of the Century
lastLast array element{jsonPath(json.store "$.book[*].title.last()" "item")}The Lord of the Rings
sumSum of numbers in the array{jsonPath(json.store "$.book[*].price.sum()" "item")}53.92
avgAverage of the numbers in the array{jsonPath(json.store "$.book[*].price.avg()" "item")}13.48
lengthNumber of elements in the array{jsonPath(json.store "$.book.length()" "item")}4