Let say I have a JSON file myfile.json with this content:
{
"vm": {
"MyVal1": false,
"MyVal2": true,
"MyVal3": "some string",
}
}
And my Jenkins pipeline looks like this:
pipeline {
agent any
options {
ansiColor('xterm')
}
stages {
stage('Parse VM details') {
steps {
echo "Read VM JSON file"
script {
def props = readJSON file: 'myfile.json'
env.MYVAL1 = props['vm'].get("MyVal1")
env.MYVAL2 = props['vm'].get("MyVal2")
env.MYVAL3 = props['vm'].get("MyVal3")
echo "MyVal1 will be: ${env.MYVAL1}"
echo "MyVal2 will be: ${env.MYVAL2}"
echo "MyVal3 will be: ${env.MYVAL3}"
}
}
}
}
}
When I run the pipeline, I got this results:
[Pipeline] echo
MyVal1 will be: null
[Pipeline] echo
MyVal2 will be: true
[Pipeline] echo
MyVal3 will be: some string
So for MyVal2
, I get back the boolean as true
, but for MyVal1
, it is always null
, whatever I try. I tried env.MYVAL1 = toBoolean(props['vm'].get("MyVal1"))
but it did not help.
Let say the JSON file is not too flexible, so I need to solve it in the Jenkins side. How could I make MyVal1
to appear as boolean even if it is false
?