[groovy-user] Tips on modifying entry in json?

519 views
Skip to first unread message

bkarsh

unread,
May 26, 2014, 9:19:18 PM5/26/14
to us...@groovy.codehaus.org
Hi wise ones,

I'm a groovy newbie, and I am banging my head against the wall on this. I
want to import some issue data into our ticketing system that is in json
format, but I need to add some attachment data to it first.

I was thinking about using groovy's jsonSlurper to slurp the JSON in, insert
the attachment(s) into the map somehow, and then rebuild the json via
jsonBuilder. Getting stuck on syntax.

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 snippet of json (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" : "te...@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)

}

... but.. how do I add attachment data to the map as I am iterating through
it?

I want to add attachment path information to each of the issues in the json
dump file -- something like this:

"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"
}
]



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

(note -- size of json file, number of issues unknown -- could be quite
large).

Also, there are admittedly several tools for importing data into Jira. I
realize this may not be best approach. I am just trying to understand this
particular approach.

Thanks for any advice!

-Bryan




--
View this message in context: http://groovy.329449.n5.nabble.com/Tips-on-modifying-entry-in-json-tp5719806.html
Sent from the groovy - user mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email


Cédric Champeau

unread,
May 27, 2014, 4:20:48 AM5/27/14
to us...@groovy.codehaus.org
Hi!

Given the input you have given, this should work:

import groovy.json.*
 
def inputFile = new File("issues.json")
def outputFile = new File("issues-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)
}

Best regards,

bkarsh

unread,
May 27, 2014, 11:27:01 AM5/27/14
to us...@groovy.codehaus.org
Thanks Cedric -- that worked like a charm. Appreciate the help!

-b



--
View this message in context: http://groovy.329449.n5.nabble.com/Tips-on-modifying-entry-in-json-tp5719806p5719814.html
Reply all
Reply to author
Forward
0 new messages