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

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
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published May 30, 2018 in Marketplace Apps

Three tips for boosting your board's efficiency with Story Maps

Trello is one of the most effective tools for driving your sprints. It's customizable for every Agile team and product owners and Scrum masters (SM) love it. However, Agile teams often struggle with:...

829 views 2 9
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