Converting a CSV string to a JSON object array when JSON Smart Value Functions do not work in an Automation Rule
Summary
In some situations, the Automation Rule Smart Value JSON Functions do not work when operating on a provided value from a Custom Field.
There is a workaround utilizing Text String Smart Value Functions that can be used to translate the CSV Strings provided into JSON object arrays.
Cause
Some features of Jira may not fully support JSON Functions. As always, Atlassian works to develop better solutions and integrations for these issues.
One example involves Assets for JSM, where an Attribute with a Type of Select and a Cardinality with a Maximum set to >1 will return a CSV String that can't be operated on using List or JSON Smart Value Functions.
Solution
There are a wide array of Text String Smart Value Functions that can be used to functionally achieve the same effect as the JSON Smart Value Functions. The primary Functions for this purpose are:
- .replace()
- .replaceAll()
- .reverse()
- .concat()
Using these 4 Functions, we can turn a Text String like this:
Bob Wilcox,John Doe,Grizz Frix
Into this JSON Object Array:
[ {"value": "Bob Wilcox"}, {"value": "John Doe"}, {"value": "Grizz Frix"} ]
This can look complicated, but the following will achieve that goal, where CSVString is the Text String above:
CSVString.replace(",","\"},{\"value\": \"").concat("\"} ]").reverse().concat("\" :\"eulav\"{ [").reverse()
To break down what's happening here:
- The first Replace() is changing the commas to this: },{"value": "
- The first Concat() is appending "} ] to the end of the string
- The first Reverse() is reversing the string so that Concat() can prepend instead of append
- The next Concat() is prepending [ {"value": " to the string, but doing so backwards so that when it is Reversed later, it will be in the correct orientation
- The next Reverse() is returning the entire string to its original orientation
With that, the transformation is complete and the JSON Object Array can be sent along, such as with a Send Web Request Action.
Notes
- The example series of Functions does not handle blank values. Meaning, if the CSV String has two commas next to each other due to a missing value, like so: ,, that is not handled by this and will create an entry in the array where value = null.
This is easily handled by a .replace(",,",",") at the beginning to remove any blanks. - This is only necessary where Values in Custom Fields or other aspects of Jira are not handled by JSON Smart Value Functions. We have open Feature Requests, such as this one: - JSDCLOUD-11392Getting issue details... STATUS that would resolve this issue for some cases.
- In situations where the Value isn't being treated as a Text String, and so the Text String Functions like .replace() do not work, this can be worked around by using a Create Variable Action to assign the value to a Variable, which should convert it to a Text String type, and allow these functions to operate on the value.
- Jira does not currently have a Smart Value Text String Function for Prepending Text, but .reverse().concat("txet").reverse() can suitably achieve the desired result of prepending.
Further Reading
- Jira Smart Values - JSON Functions
- Jira Smart Values - Text Fields
- Jira Automation Actions - Create Variable