Skip to content

Instantly share code, notes, and snippets.

@sagrawal31
Created April 13, 2021 07:35
Show Gist options
  • Save sagrawal31/2571df62f173345be9819ed2fab0cd46 to your computer and use it in GitHub Desktop.
Save sagrawal31/2571df62f173345be9819ed2fab0cd46 to your computer and use it in GitHub Desktop.
Convert string to any actual desired format in Groovy
import groovy.json.*
Object getValue(String value) {
try {
JsonSlurper slurper = new JsonSlurper()
return slurper.parseText(value)
} catch (JsonException e) {
// supress the exception - means it's unparceable
}
if (value == "N") {
return false
} else if (value == "Y") {
return true
} else {
log(">>> WARN: Unparceaable value $value")
return value
}
}
println getValue("4").dump()
println getValue("4.6").dump()
println getValue("N").dump()
println getValue('{"id": "4.5"}').dump()
println getValue("[2,3,4]").dump()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment