Skip to content

Instantly share code, notes, and snippets.

@cdenneen
Last active February 23, 2018 18:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cdenneen/6c23b2a635fa013e6d578c9d7c8ccee2 to your computer and use it in GitHub Desktop.
Save cdenneen/6c23b2a635fa013e6d578c9d7c8ccee2 to your computer and use it in GitHub Desktop.
Jenkins parse json
//@NonCPS
def parseText(txt){
return new groovy.json.JsonSlurperClassic().parseText(txt)
}
node ('master') {
variables = parseText(new File('/tmp/test.json').text)
// this works
def out = variables.dne.prod.dashboard.stacknameprefix
print out
// this also works
print variables.dne.prod.dashboard.stacknameprefix
// and this also works
def var = 'dne.prod'
print evaluate('variables.' + var + '.dashboard.stacknameprefix')
// or this
def val = 'dne.prod'
def map_file = '/tmp/test.json'
def map = readJSON file: map_file
def string = 'dne.prod'
// this is currently broken in jenkins... patching coming
//def (one, two) = string.tokenize('.')
def array = string.tokenize('.')
def val1 = 'dne'
def val2 = 'prod'
def map_file = '/tmp/test.json'
def map = readJSON file: map_file
print map."${val1}"."${val2}".dashboard.stacknameprefix
print map."${array[0]}"."${array[1]}".dashboard.stacknameprefix
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment