Created
August 17, 2017 13:45
-
-
Save mig82/a647272ce2bc07610a5eff6967442dd8 to your computer and use it in GitHub Desktop.
How to parse json into a serializable HashMap in Jenkins 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
@NonCPS | |
def parseJson(txt){ | |
def lazyMap = new groovy.json.JsonSlurper().parseText(txt) | |
def map = [:] | |
for ( prop in lazyMap ) { | |
map[prop.key] = prop.value | |
} | |
return map; | |
} |
def foo( String txt ){
def object = new groovy.json.JsonSlurper().parseText(txt)
Map.isCase( object ) ? object.collectEntries {[ it.key, it.value ]} :
List.isCase( object ) ? object.collect { it } : object
}
@marslo nice! you forgot recursion though
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Another small improvement (use generic Map and List rather than specific implementations):