Skip to content

Instantly share code, notes, and snippets.

@leolin310148
Created June 14, 2015 09:49
Show Gist options
  • Save leolin310148/e108cd8cf79a19ae8800 to your computer and use it in GitHub Desktop.
Save leolin310148/e108cd8cf79a19ae8800 to your computer and use it in GitHub Desktop.
Parse json to xml using groovy
import groovy.json.JsonSlurper
import groovy.xml.MarkupBuilder
def jsonString = "{" +
" \"result\": [" +
" {" +
" \"Name\": \"John\"," +
" \"Department\": \"001\"" +
" }," +
" {" +
" \"Name\": \"Mary\"," +
" \"Department\": \"002\"" +
" }," +
" {" +
" \"Name\": \"Steve\"," +
" \"Department\": \"002\"" +
" }" +
" ]" +
"}"
def parsedJson = new JsonSlurper().parseText(jsonString)
def writer = new StringWriter()
new MarkupBuilder(writer).with {
doubleQuotes = true
results{
parsedJson.result.each {employee->
result{
Name(employee.Name)
Department(employee.Department)
}
}
}
}
println writer.toString()
@yackx
Copy link

yackx commented May 31, 2018

You can use the ''' delimiter to declare multi-line String. No need for \in that case (using single quote ' delimiter also allows you to declare unprotected double quotes)

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