Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

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.

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

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

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

Using jemhUtils with a Custom Field value in JavaScript

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

Using JSON.parse with a Custom Field value in JavaScript

Code Block
JSON.parse(relatedIssue.getCustomFieldValue("customfield_10233"))

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.e.g..

Velocity jemhUtils Example:

Code Block
$jemhUtils.createJSONFromSource($issue.getCustomFieldValue("customfield_10233")).object

JavaScript jemhUtils Example:

Code Block
$jemhUtilsjemhUtils.createJSONFromSource($issue(relatedIssue.getCustomFieldValue("customfield_10233")).get("object")

JavaScript JSON.parse Example:

Code Block
JSON.parse(relatedIssue.getCustomFieldValue("customfield_10233")).object

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:

Code Block
$jemhUtils.createJSONFromSource($issue.getCustomFieldValue("customfield_10233")).object.array.name

JavaScript jemhUtils Example:

Code Block
jemhUtils.createJSONFromSource(relatedIssue.getCustomFieldValue("customfield_10233")).get("object").get("array").get("name")

JavaScript JSON.parse Example:

Code Block
JSON.parse(relatedIssue.getCustomFieldValue("customfield_10233")).object.array.name

JSON Object example:

Code Block
{"object": {
        "name": "test",
        "array": {
            "name": "ArrayObject Item1",
            "id": "10003",

         },
    }
}

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:

Code Block
$jemhUtils.createJSONFromSource($issue.getCustomFieldValue("customfield_10233")).object.array.get(0).name

JavaScript jemhUtils Example:

Code Block
jemhUtils.createJSONFromSource(relatedIssue.getCustomFieldValue("customfield_10233")).get("object").get("array").get(0).get("name")

JavaScript JSON.parse Example:

Code Block
JSON.parse(relatedIssue.getCustomFieldValue("customfield_10233")).object.array[0].name

JSON array example:

Code Block
{"object": {
        "name": "test",
        "array": [
          {
            "name": "Array Item1",
            "id": "10003",
          },
         {
            "name":"Array Item2",
            "id": "10004",
          }
        ]
    }
}