jenkins-pipeline readJSON - how to access nested element

Viewed 4844
1 Answers

You can always get the value of a nested element by its nested key using bracket notation or dot notation.

stage('Read-JSON') {
    steps {
        script {
            def oldJson = '{"branch":{"type-0.2":{"version":"0.2","rc":"1","rel":"1","extras":"1"}}}'
            def props = readJSON text: oldJson
            println(props['branch']['type-0.2']['rc'])
            \\ or println(props.'branch'.'type-0.2'.'rc')

        }
    }
}

Output:

[Pipeline] stage
[Pipeline] { (Read-JSON)
[Pipeline] script
[Pipeline] {
[Pipeline] readJSON
[Pipeline] echo
1
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
Related