Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot read Json value by key in JenkinsFile

I am defining my environment variables in Jenkins-File. I am using Pipeline Utility Steps Plugin to read a json file in the directory that has configuration. When I echo out the read json file, the output is correct, it reads and prints the json file correctly. When I try to access the value associated with a key in that json object, I am getting error: "No such property: internalCentralConsoleUrl for class: java.lang.String"

The json format config file looks life following:

{
    "activeVersion": "19.01.303",
    "internalCentralConsoleUrl": "https://11.111.111:8083/api/v1",
    "dataType": "APPLICATION_JSON"
}

I am reading that file using readJSON in the pipeline. And in the following lines, trying to access the value inside the json object using the key. Which gives the error I mentioned above.

pipeline {
agent any
environment { 
        config = readJSON file: 'config.json'
        baseUrl = "${config.internalCentralConsoleUrl}"
        baseUrl2 = config['internalCentralConsoleUrl']
        }
stages {}
}

Both the ways I tried above to read the json value are documented in the jenkins page linked here

I cannot wrap my head around what is causing an issue in this straight forward task.

Edit1: Just corrected a formatting mistake in pipeline.

like image 819
Virodh Avatar asked Nov 20 '25 02:11

Virodh


1 Answers

I copied your example and added a stage to print the variable:

pipeline {
    agent any
    environment {
        def config = readJSON file: 'config.json'
        baseUrl = "${config.internalCentralConsoleUrl}"
    }
    stages {
        stage('Test') {
            steps {
                echo baseUrl
            }
        }
    }
}

And it prints the variable correctly without any exception:

[Pipeline] {
[Pipeline] readJSON
[Pipeline] readJSON
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Test)
[Pipeline] echo
https://11.111.111:8083/api/v1
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
like image 52
RNoB Avatar answered Nov 23 '25 06:11

RNoB



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!