Advice on modifying json import file with Groovy

Bryan Karsh
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 26, 2014

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

1 vote
Answer accepted
Bryan Karsh
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 26, 2014

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
TAGS
AUG Leaders

Atlassian Community Events