How to programmatically update Issues from a JQL using REST API in Jira

Still need help?

The Atlassian Community is here for you.

Ask the community

Platform Notice: Cloud, Server, and Data Center - This article applies equally to all platforms.

Support for Server* products ended on February 15th 2024. If you are running a Server product, you can visit the Atlassian Server end of support announcement to review your migration options.

*Except Fisheye and Crucible

  

Summary

This article proposes a solution to update Issues that match a JQL using Jira's REST API.


Environment

All versions of Jira Core & Software 7 and 8.

All versions of Jira Service Manager 3 and 4.

Jira Cloud.


Solution

The solution is to first search for the issues and then, for each of them, call the update. This would result in "1 + N" REST API calls, N being the number of issues returned by the JQL.

  1. Perform the JQL search through POST or GET to /rest/api/2/search (Server/DC API reference | Cloud API reference)
  2. Parse the result to you get only the issue keys returned
  3. For each issue key, call the update endpoint through PUT to /rest/api/2/issue/{issueIdOrKey} (Server/DC API reference | Cloud API reference)


Examples in shell script

  1. Search issues with the JQL

    Command
    $ curl -u admin:admin -X POST localhost:50813/rest/api/2/search -H "Content-Type: application/json" -d '{"jql":"updated > -1d","fields":[""]}' -s | jq > jql-output.txt
    Sample output
    {
      "expand": "schema,names",
      "startAt": 0,
      "maxResults": 50,
      "total": 10,
      "issues": [
        {
          "expand": "operations,versionedRepresentations,editmeta,changelog,renderedFields",
          "id": "10108",
          "self": "http://localhost:50813/rest/api/2/issue/10108",
          "key": "SC1-10"
        },
        {
          "expand": "operations,versionedRepresentations,editmeta,changelog,renderedFields",
          "id": "10107",
          "self": "http://localhost:50813/rest/api/2/issue/10107",
          "key": "SC1-9"
        },
        {
          "expand": "operations,versionedRepresentations,editmeta,changelog,renderedFields",
          "id": "10106",
          "self": "http://localhost:50813/rest/api/2/issue/10106",
          "key": "SC1-8"
        }
      ]
    }

     
     

  2. Parse the json output

    Command
    $ cat jql-output.txt | grep '"key"' | egrep -o ': ".*?"' | cut -c3- | sed 's/"//g' > keys.txt
    Sample output
    SC1-10
    SC1-9
    SC1-8

     
     

  3. For each issue key, update the issue

    Command
    $ for issue in $(cat keys.txt);  do curl -u admin:admin -X PUT localhost:50813/rest/api/2/issue/$issue -H "Content-Type: application/json" -d '{"fields":{"customfield_10400":"Sample text for a Text Field!"}}';  done;

     
     

  4. Alternative one-line example

    Command
    $ for issue in \
      $(curl -u admin:admin -X POST localhost:50813/rest/api/2/search -H "Content-Type: application/json" -d '{"jql":"updated > -1d","fields":[""]}' -s | jq | grep '"key"' | egrep -o ': ".*?"' | cut -c3- | sed 's/"//g');\
      do curl -u admin:admin -X PUT localhost:50813/rest/api/2/issue/$issue -H "Content-Type: application/json" -d '{"fields":{"customfield_10400":"Sample text for a Text Field!"}}';\
      done;


    For all command examples above:

    Replace thisFor this
    admin:admin

    Valid username:password

    (with browse and edit permissions in the projects)

    localhost:50813Jira's base URL
    updated > -1dThe desired JQL
    customfield_10400The customfield to update


For more examples on how to edit issues through the REST API, like updating multiple fields, check the Server/DC REST API edit examples or the Cloud REST API edit examples!

Last modified on Dec 12, 2020

Was this helpful?

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