Converting a CSV string to a JSON object array when JSON Smart Value Functions do not work in an Automation Rule

Still need help?

The Atlassian Community is here for you.

Ask the community


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:

  1. .replace()
  2. .replaceAll()
  3. .reverse()
  4. .concat()

Using these 4 Functions, we can turn a Text String like this:

CSV String
Bob Wilcox,John Doe,Grizz Frix

Into this JSON Object Array:

JSON 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:

Smart Value Functions
CSVString.replace(",","\"},{\"value\": \"").concat("\"} ]").reverse().concat("\" :\"eulav\"{ [").reverse()

To break down what's happening here:

  1. The first Replace() is changing the commas to this: },{"value": "
  2. The first Concat() is appending "} ] to the end of the string
  3. The first Reverse() is reversing the string so that Concat() can prepend instead of append
  4. 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
  5. 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-11392 - Getting 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


Last modified on Jan 5, 2023

Was this helpful?

Yes
No
Provide feedback about this article
Powered by Confluence and Scroll Viewport.