Advice on modifying json import file with Groovy

Hi guys,

I've been playing around with the JSON export/import functionality. It seems like a great way to go, except it seems to lack Attachment file location in the JSON.

Here is my question. I was thinking about using groovy's jsonSlurper to slurp the JSON in, modify the attachment(s) in the map, and then rebuild json via jsonBuilder.

Anyone do this before? I am relatively new to Groovy.. having a hard time trying to edit attachments in place.

For example, here is example issue (note -- I know the json may be borked -- I removed sections to make example smaller):

  "links" : [ ],
  "projects" : [ {
    "externalName" : "Test Queue",
    "name" : "Test Queue",
    "key" : "TEST",
    "lead" : "TEST_triage",
    "description" : "This is a test.\n\n",
    "projectCategoryName" : "EXAMPLE",
    "assigneeType" : 2,
    "versions" : [ ],
    "components" : [ {
      "name" : "Foo",
      "lead" : "Foo Lead"
    } ],
    "issues" : [ {
      "key" : "TEST-10748",
      "summary" : "this is a test - please disregard.",
      "reporter" : "bryank",
      "assignee" : "bryank",
      "description" : "test",
      "issueType" : "Work Item",
      "status" : "Open",
      "priority" : "P3",
      "created" : 1401119754000,
      "updated" : 1401119754000,
      "labels" : [ ],
      "worklogs" : [ ],
      "voters" : [ ],
      "watchers" : [ "bryank" ],
      "subtasks" : [ ],
      "attachments" : [ ],
      "history" : [ ],
      "comments" : [ ],
      "customFieldValues" : [ ]
    } ]
  } ],
  "users" : [ {
    "name" : "test_triage",
    "fullname" : "Test Triage",
    "email" : "",
    "groups" : [ "jira-users"],
    "active" : true

If I slurp the json in, I can access sections like so:

import groovy.json.*

def inputFile = new File("C:\\Users\\bryank\\Desktop\\ticket.json")
def InputJSON = new JsonSlurper().parseText(inputFile.text)

List issues = InputJSON.projects.issues

issues.each { issue ->


I basically want to add attachment path information to each of the issues in the json dump file (note -- size of file, number of issues unknown). I read I should be using collect with the closure.. but I can't get the syntax down right.

When done, I want to rebuild into json, and test importing into jira.

I totally realize there may be much easier ways to go about this (csv attachment upload, jira cli, etc) -- this is more out of curiousity. File this under ("I'd really like to know how to modify json files prior to upload"))

1 answer

1 accepted

1 vote
Accepted answer

Got this example from Cedric Champeau at works like a charm:

import groovy.json.*

def inputFile = new File("C:\\Users\\bryank\\Desktop\\ticket.json")
def outputFile = new File("C:\\Users\\bryank\\Desktop\\ticket-out.json")
def json = new JsonSlurper().parseText(inputFile.text)

json.projects.each { p ->
    p.issues.each { issue ->
        issue.attachments = [[
                name: "battarang.jpg",
                attacher: "admin",
                created: "2012-08-31T17:59:02.161+0100",
                uri: "http://optimus-prime/~batman/images/battarang.jpg",
                description: "This is optimus prime"
def builder = new JsonBuilder(json)
println builder.toPrettyString()
outputFile.withWriter {

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Nov 29, 2018 in Marketplace Apps

How to set up an incident workflow from the VP of Engineering at Sentry

Hey Atlassian community, I help lead engineering at Sentry, an open-source error-tracking and monitoring tool that integrates with Jira. We started using Jira Software Cloud internally last year, a...

1,711 views 3 11
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you