This article discusses how to effectively access and manipulate JSON data using smart values and JSON functions in Atlassian Automation Cloud. Whether you are working with data from an "Incoming webhook" or preparing to send data through the "Send web request" action, mastering these techniques is essential for optimising your automation rules.
To output the evaluated smart value expressions, use the "Log action" component. This writes the evaluated expression to the rule's "Audit log" and will be used throughout the article.
When data is received through an "Incoming webhook" the smart value {{webhookData}}
is automatically converted into a JSON object. However, sometimes the data in other smart values remains in a text/string format.
Below is an example JSON string used throughout this article.
{ "message": { "subject": "Test message", "participants": { "from": "from.test@example.com", "to": [ "to.test1@example.com", "to.test2@example.com" ] }, "attachments": [ { "filename": "attachment1.txt", "tags": [ "tag1", "tag2" ], "insights": [ { "type": "quality", "value": "insight1 value" }, { "type": "safety", "value": "insight2 value" }, { "type": "safety", "value": "insight5a value" } ] }, { "filename": "attachment2.txt", "tags": [ "tag3", "tag4" ], "insights": [ { "type": "quality", "value": "insight3 value" }, { "type": "safety", "value": "insight6a value" } ] } ] } }
This string may originate from a smart value variable you defined or as part of a Jira issue's data. Below is an example of a Jira issue description containing this string in JSON format.
For convenience, here is the JSON in a single-line format (for you to copy and paste):
{"message":{"subject":"Test message","participants":{"from":"from.test@example.com","to":["to.test1@example.com","to.test2@example.com"]},"attachments":[{"filename":"attachment1.txt","tags":["tag1","tag2"],"insights":[{"type":"quality","value":"insight1 value"},{"type":"safety","value":"insight2 value"},{"type":"safety","value":"insight5a value"}]},{"filename":"attachment2.txt","tags":["tag3","tag4"],"insights":[{"type":"quality","value":"insight3 value"},{"type":"safety","value":"insight6a value"}]}]}}
We can access that data using {{issue.description}}
. To work with JSON data, we first convert the string to JSON using the jsonStringToObject() function.
Smart value expression:
{{jsonStringToObject(issue.description)}}
Evaluated output:
{message={subject=Test message, participants={from=from.test@example.com, to=[to.test1@example.com, to.test2@example.com]}, attachments=[{filename=attachment1.txt, tags=[tag1, tag2], insights=[{type=quality, value=insight1 value}, {type=safety, value=insight2 value}, {type=safety, value=insight5a value}]}, {filename=attachment2.txt, tags=[tag3, tag4], insights=[{type=quality, value=insight3 value}, {type=safety, value=insight6a value}]}]}}
Once the text/string is converted into a JSON object, you can access its data using dot notation.
Smart value expression:
{{jsonStringToObject(issue.description).message.participants.from}}
Evaluated output:
from.test@example.com
You can access an array even when it is nested within a structure.
Smart value expression:
{{jsonStringToObject(issue.description).message.participants.to}}
Evaluated output:
to.test1@example.com, to.test2@example.com
You can branch through the array and access each item individually.
Smart value expression (for branch):
{{jsonStringToObject(issue.description).message.participants.to}}
Smart value expression (for branched item):
PT: {{participantTo}}
Evaluated output:
PT: to.test1@example.com, PT: to.test2@example.com
When accessing multiple arrays, they remain separate and are not merged into a single array.
Smart value expression (for branch):
{{jsonStringToObject(issue.description).message.attachments.tags}}
Evaluated output:
[tag1, tag2], [tag3, tag4]
You can branch through that array, but each item will be an array itself, rather than individual elements within those arrays.
Smart value expression (for branch):
{{jsonStringToObject(issue.description).message.attachments.tags}}
Smart value expression (for branched item):
TAG_ARRAY: {{tagArray}}
Evaluated output (looped twice):
TAG_ARRAY: tag3, tag4, TAG_ARRAY: tag1, tag2
These are two arrays and not four separate items.
If you want to merge multiple arrays into a single array, use the flatten
function.
Smart value expression:
{{jsonStringToObject(issue.description).message.attachments.tags.flatten}}
Evaluated output:
tag1, tag2, tag3, tag4
You can now branch over it and access each individual item.
Smart value expression (for branch):
{{jsonStringToObject(issue.description).message.attachments.tags.flatten}}
Smart value expression (for branched item):
TAG_ARRAY: {{tagArray}}
Evaluated output (looped four times):
TAG_ARRAY: tag4, TAG_ARRAY: tag2, TAG_ARRAY: tag3, TAG_ARRAY: tag1
The same can be done for accessing objects in a nested array.
Smart value expression:
{{jsonStringToObject(issue.description).message.attachments.insights.flatten}}
Evaluated output:
{type=quality, value=insight1 value}, {type=safety, value=insight2 value}, {type=safety, value=insight5a value}, {type=quality, value=insight3 value}, {type=safety, value=insight6a value}
This enables you to branch through these objects and filter using a condition component.
Smart value expression (for branch):
{{jsonStringToObject(issue.description).message.attachments.insights.flatten}}
Added condition where type {{myInsightObject.type}}
equals "safety".
Smart value expression (for branched item):
Safety: {{myInsightObject.value}}
Evaluated output:
Safety: insight2 value, Safety: insight5a value, Safety: insight6a value
Currently, nested branching is not supported, and data within a branch cannot be shared outside of it, limiting its use for filtering.
To work around this limitation, you can filter data while creating the smart value variable using "Create variable." Let's name the variable safetyInsights
.
Smart value expression (for branch):
{{#jsonStringToObject(issue.description).message.attachments.insights.flatten}}{{#if(equals(type, "safety"))}}{{value}}{{^last}},{{/}}{{/}}{{/}}
Evaluated output:
insight2 value,insight5a value,insight6a value
Ensure that you’re not adding unintended spaces in the expression, as this creates a single string where any spaces in your expression will be preserved.
This expression filters and returns only the items where the type is equal to "safety".
Reminder: Once created as a variable, it is no longer recognised as an array but as a string/text. To convert it back into an array for branching, use the split
function.
Smart value expression (for branch):
{{safetyInsights.split(",")}}
You can now access each item within the branch using the defined variable name.
Smart value expression (for branched item):
Item:{{singleSafetyInsight}}
Evaluated output:
Item:insight5a value, Item:insight2 value, Item:insight6a value
Enjoy your rule creation!
Christopher
2 comments