Skip to content

Instantly share code, notes, and snippets.

Created May 22, 2013 18:14
What would you like to do?
Convert xml to pojo to json using groovy
import groovy.json.JsonBuilder
//make sure you have installed groovy
//best way to install groovy:
def carRecords = '''
<car name='HSV Maloo' make='Holden' year='2006'>
<record type='speed'>Production Pickup Truck with speed of 271kph</record>
<car name='P50' make='Peel' year='1962'>
<country>Isle of Man</country>
<record type='size'>Smallest Street-Legal Car at 99cm wide and 59 kg in weight</record>
<car name='Royale' make='Bugatti' year='1931'>
<record type='price'>Most Valuable Car at $15 million</record>
def xmlRecords = new XmlSlurper().parseText(carRecords)
def jsonObject = [:]
jsonObject.records = []
def records = jsonObject.records {xmlCar ->
records.add([car:[name:xmlCar.@name.text(), country:]])
def builder = new JsonBuilder(jsonObject)
println builder.toPrettyString()
Copy link

Sorry Peri, wasn't as slick as I hoped

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment