Created
June 14, 2015 09:49
-
-
Save leolin310148/e108cd8cf79a19ae8800 to your computer and use it in GitHub Desktop.
Parse json to xml using groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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)