Working with JSON Data

As happens from time to time, you will want to work with JSON data. For example, when passing JSON data when using httpPut() or httpPost().

Background

In this example, I will be using the this REST API example from the Atlassian Community.

Method 1 - Add JSON Data Inline

As is shown in the example, we start with the example JSON that will be used to input.

"properties": [    {     "key": "sd.public.comment",     "value": {        "internal": true     }    }   ] }

Overview

  1. Use the Find/Replace feature of the SIL Manager to add an escape character before every quote.

  2. Surround each line with quotes and place a plus sign at the end.

  3. Set the newly formatted JSON to a string variable and put a semicolon at the end.

Step 1 - Add an Escape Character before Every Quote

Use the Find/Replace feature of the SIL Manager to add an escape character before every quote.

Step 2 - Place Quotes Around Each Line

Surround each line with quotes and place a plus sign at the end. Place a semicolon at the end.

Step 3 - Set the Formatted JSON to a String Variable

Set the entire code block to a string variable so that it can be passed into the REST endpoint.

string updateInfo = "{" +                         "\"properties\": [" +                             "{" +                                 "\"key\": \"sd.public.comment\"," +                                 "\"value\": {" +                                     "\"internal\": true" +                                 "}" +                             "}" +                         "]" +                     "}";                       runnerLog(updateInfo);

 

Step 4 - Print Output and Check for Accuracy

The output should match the original JSON from the beginning.

Step 5 - Change Desired Hard-Coded Values to Accept Variables

Add a variable and use plus signs to concatenate it to the existing string values. Again, the output should match the original JSON from the beginning.

The Final Script

Below we use httpPut() to pass the "updateInfo" values to update the Service Desk value "isPublic" to false.

string isPublic = "false"; number [] ids = getAllCommentIds(key); number lastComment = ids[size(ids) -1];     string updateInfo = "{" +                         "\"properties\": [" +                         "{" +                         "\"key\": \"sd.public.comment\"," +                         "\"value\": {" +                             "\"internal\": " + isPublic + "" +                         "}" +                         "}" +                         "]" +                     "}";     HttpRequest request;  request.headers += httpCreateHeader("Content-Type", "application/json");  request.headers += httpBasicAuthHeader("admin", "admin");     string result = httpPut("http://localhost:8080/rest/api/3/issue/" + key + "/comment/" + lastComment, request, updateInfo);   // runnerLog(result);   number statusCode = httpGetStatusCode(); if (statusCode >= 200 && statusCode < 300) {     runnerLog("SUCCESS"); } else {     string msg = trim(statusCode) + ":" + httpGetErrorMessage() + ":" + httpGetReasonPhrase();     runnerLog(msg); }

Additional Help

Need help implementing this script? Talk to me directly to me by clicking on the bot on this page.

Related articles

Filter by label

There are no items with the selected labels at this time.