Click here to Skip to main content
15,895,799 members
Articles / Programming Languages / XML
Tip/Trick

Calling Rest APIs in Jira using JavaScript

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
31 Aug 2016CPOL1 min read 23.4K   3
This article explains how to write JavaScript code in Atlassian Jira to call external REST APIs and load json data into Jira custom fields

This article appears in the Third Party Products and Tools section. Articles in this section are for the members only and must not be used to promote or advertise products in any way, shape or form. Please report any spam or advertising.

Introduction

This article provides an entry point to write JavaScript code in Atlassian Jira to call external REST APIs and load json data into Jira custom fields.

Background

There are scenarios where we want data from a 3rd party application (example: Java or .NET) to be displayed in Jira's custom fields.

This can be achieved by writing a JavaScript code using XMLHttpRequest.

Using Code

We will use the below external API for this article, this API is free and easily available over the net. You can click on the link and see the json data getting generated.

Suppose we want to display json data from the above mentioned API link into 3 fields - Proposed Project Name, Project Sponsor ADS ID and 3rd Party Name (see below screenshot).

Image 1

We can write JavaScript for the same in any of the custom field's "Field Configuration" present on Create Issue screen of Jira.

Below is the JavaScript code snippet.

Image 2

Code Analysis

  1. Here, we are creating an object of XMLHttpRequest.
    XMLHttpRequest is an API that provides client functionality for transferring data between a client and a server. It provides an easy way to retrieve data from a URL without having to do a full page refresh.
  2. Open the given API link with open() function.
  3. Parse the json data generated by calling the API link into a variable "obj".
  4. Update Jira custom fields with the parsed json data.
    P.S.: You can know the ID of any customfield by Right Click -> Inspect element

Below is the JavaScript code for your reference:

JavaScript
<script type="text/javascript"> 

var xhr = new XMLHttpRequest();
xhr.open("GET", "https://teamtreehouse.com/matthew.json", false);
xhr.send();
var obj = JSON.parse(xhr.responseText);
document.getElementById("customfield_10800").value = obj.badges[1].id;
document.getElementById("customfield_10801").value = obj.badges[1].name; 
document.getElementById("customfield_10802").value = obj.badges[1].url; 
console.log(xhr.status);
console.log(xhr.statusText);

</script>

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India
Aditya Verma
Working in a MNC as a Developer/Administrator for Atlassian suite of Products like Jira, Confluence.
Find me on LinkedIn here

Comments and Discussions

 
GeneralMy vote of 5 Pin
MayurDighe6-Sep-16 7:13
professionalMayurDighe6-Sep-16 7:13 
GeneralMy vote of 5 Pin
Member 1271315631-Aug-16 5:18
Member 1271315631-Aug-16 5:18 
PraiseWell explained Pin
Member 1271315631-Aug-16 5:17
Member 1271315631-Aug-16 5:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.