Using Custom Field JSON values within Scripts/Templates

Summary - SINCE 4.1.27

When using some Custom Fields they may return a JSON Object as the value which made it difficult to extract the relevant value. Within JEMH 4.1.27 we have added a new method to jemhUtils which allows to manipulate JSON values more easily. This page will highlight how to extract specific information from the JSON Object.

Note: Within JavaScript features (e.g. Script Field Processor) there is a default Java method that can also be used (JSON.parse()) to treat the text value as a JSON Object. This page will also cover using this method.

Converting a Custom Field value into a JSON value

When we gather the value from the Custom Field it will be returned as a String value which means that it is currently not possible to manipulate the JSON data. To be able to access the JSON data you will need to use the following command to convert the Text value into a JSON value.

Velocity - $jemhUtils.createJSONFromSource(String jsonSource) JavaScript - jemhUtils.createJSONFromSource() JavaScript intergrated - JSON.parse()

Using jemhUtils with a Custom Field value in Velocity (Templates)

$jemhUtils.createJSONFromSource($issue.getCustomFieldValue("customfield_10233"))

Using jemhUtils with a Custom Field value in JavaScript

jemhUtils.createJSONFromSource(relatedIssue.getCustomFieldValue("customfield_10233"))

Using JSON.parse with a Custom Field value in JavaScript

Extracting a Specific Object value

To extract a specific Object you will simply need to add the object name to the above and this will return the value/s for that object.

Velocity jemhUtils Example:

JavaScript jemhUtils Example:

JavaScript JSON.parse Example:

To then get the value of the next object, you will need to add the next object name. e.g. below will return Array Item1

Velocity jemhUtils Example:

JavaScript jemhUtils Example:

JavaScript JSON.parse Example:

JSON Object example:

Extracting a specific value from a JSON Array

Sometimes the JSON object may contain an array for a specific object. To access the array value you must specify the Array to get the value by using .get() and then specifying the Array number e.g. below will return the name that is found within first array in the list (e.g.Array Item1)

Velocity jemhUtils Example:

JavaScript jemhUtils Example:

JavaScript JSON.parse Example:

JSON array example: