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" : "test@int.test.com",
    "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 ->
println(issue.key)

}

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

This widget could not be displayed.

Got this example from Cedric Champeau at nabble.com-- 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 {
    builder.writeTo(it)
}

Suggest an answer

Log in or Sign up to answer
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Published Jul 25, 2018 in Marketplace Apps

Jira Cloud and Bitbucket Cloud Integration with Microsoft Teams

One of the newest products in the Microsoft family - Microsoft Teams,  is a chat-based hub for teamwork that integrates all the people, content, and tools your team needs to be more engaged and ...

721 views 0 3
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