Home > Software engineering >  Calling each key and value in json in groovy pipeline
Calling each key and value in json in groovy pipeline

Time:01-10

I have a task to be able to read a json file from a gitlab and then called the json file based on each envioronment selected in the parameter. For example now, I have Upgrade and BV, and Upgrade is selected, it should be able to read based on this key value pairing below:

Expected value to get from the json

  $.[env].level 
$.[env].domain 
$.[env].resources 
$.[env].resources.db-schemas 
$.[env].resources.db-schemas.[name].
$.[env].resources.db-schemas.[name].
$.[env].resources.db-schemas.[name].
$.[env].resources.db-schemas.[name].jvm 
$.[env].jvm 
$.[env].jvm.[type] 
$.[env].jvm.[type](.jvm-name) 
$.[env].jvm.[type](.jvm-name).jenkins-name 
$.[env].jvm.[type](.jvm-name).host 
$.[env].jvm.[type](.jvm-name).ip 
$.[env].jvm.[type](.jvm-name.port) 

The $.[env] is the params that might be selected either upgrade or BV.

The json file contains :

   {
    "upgrade": {
        "level": 1,
        "domain": "develop.autosample.co.uk",
        "resources": {
            "db-schemas": {
                "rule": {
                    "schema": "pegarules",
                    "database": "sas_develop",
                    "jvm": ["primary", "secondary"]
                },
                "data": {
                    "schema": "pegadata",
                    "database": "sas_develop",
                    "jvm": ["primary", "secondary"]
                },
                "report": {
                    "schema": "pegareports",
                    "database": "sas_develop",
                    "jvm": ["primary", "secondary"]
                }
            }
        },
        "jvm": {
            "load-balancer": null,
            "pega": [{
                "jenkins-name": "ent-giem-sasw02",
                "host": "ent-giem-sasw02",
                "ip": "x.x.x.x"
            }],
            "db": {
                "primary": {
                    "jenkins-name": "ent-giem-sasrd26",
                    "host": "ent-giem-sasrd26",
                    "ip": "x.x.x.x",
                    "port": 5432
                },
                "secondary": {
                    "jenkins-name": "ent-giem-sasrd98",
                    "host": "ent-giem-pgrd98",
                    "ip": "x.x.x.x",
                    "port": 5432
                }
            }

        }
    },

    "BV": {
        "level": 1,
        "domain": "BV.autosample.co.uk",
        "resources": {
            "db-schemas": {
                "rule": {
                    "schema": "pegarules",
                    "database": "sas_bv",
                    "jvm": ["primary", "secondary"]
                },
                "data": {
                    "schema": "pegadata",
                    "database": "sas_bv",
                    "jvm": ["primary", "secondary"]
                },
                "report": {
                    "schema": "pegareports",
                    "database": "sas_bv",
                    "jvm": ["primary", "secondary"]
                }
            }
        },
        "jvm": {
            "load-balancer": null,
            "pega": [{
                "jenkins-name": "ent-giem-sasw02",
                "host": "bv-giem-sasw02",
                "ip": "x.x.x.x"
            }],
            "db": {
                "primary": {
                    "jenkins-name": "ent-giem-sasrd26",
                    "host": "bv-giem-sasrd26",
                    "ip": "x.x.x.x",
                    "port": 5432
                },
                "secondary": {
                    "jenkins-name": "ent-giem-sasrd98",
                    "host": "bv-giem-pgrd98",
                    "ip": "x.x.x.x",
                    "port": 5432
                }
            }
        }



    }

}

The groovy code at the moment which even though is reading the json, I need to know how to call based on the key and value indicated above :

#!/usr/bin/env groovy    

node{
    properties([
    parameters([

   choice(
        name: 'environment',
        choices: ['','Upgrade','BV' ],
        description: 'environment to choose'
        ),

    ])
]) 

    deleteDir()
        dir('dir-switch') {

    stage('Checkout') {
   
   // git branch: 'test-upgrade-json', url: 'https://gitlab.xxxxxxx/pipeline.git'
  // stash includes: '**', name: 'upgrade-pega'
        
checkout([$class: 'GitSCM', branches: [[name: '*/test-upgrade-json']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'jenkins-user-github', url: 'https://gitlab.xxxxx/pipeline.git']]])
                
    }

     
            stage('Get Environment') {
            sh """
                ls -lart ./*
                ls ${env.WORKSPACE}
                cp -R ./upgrade-pega/environment/env.json  /${env.WORKSPACE}/dir-switch
                ls /${env.WORKSPACE}/dir-switch
            """
        
          }  


def obj = readJSON file: './env.json'
def list = obj[params.environment];

println list

list.each { println it }


 


stage('JVM check content') { 

/////   print each key and value in the json to be able to used in the code pipeline

            node(list.host.first()) {
     
sh """
  echo -e "-------------------------------System Information of current node running ----------------------------"
  echo -e "Hostname:\t\t"`hostname`
  echo -e "System Main IP:\t\t"`hostname -I`

""".stripIndent()

/// the text file now in the workspace in the folder dir-switch





                }
   

}


        }



}

The error am seeing is :

[Pipeline] // node
[Pipeline] End of Pipeline
hudson.remoting.ProxyException: net.sf.json.JSONException: Expected a ',' or '}' at character 1488 of {
 "upgrade": {
        "level": 1,
        "domain": "develop.autosample.co.uk",
        "resources": {
            "db-schemas":
'''''
'''''     
.....

            "port": 5432
                }
            }
        }
    }
    at net.sf.json.util.JSONTokener.syntaxError(JSONTokener.java:499)
    at net.sf.json.JSONObject._fromJSONTokener(JSONObject.java:1043)
    at net.sf.json.JSONObject._fromString(JSONObject.java:1145)
    at net.sf.json.JSONObject.fromObject(JSONObject.java:162)
    at net.sf.json.JSONSerializer.toJSON(JSONSerializer.java:139)
    at net.sf.json.JSONSerializer.toJSON(JSONSerializer.java:103)
 

CodePudding user response:

It's not a Groovy issue. Your JSON has a syntax error in it. There is a missing } to close off "upgrade" object. So this is what you posted:

{
    "upgrade": {
     ...
    "BV": {
     ...
}

What it should be is:

{
    "upgrade": {
    ...
    },
    "BV": {
    ...
    }
}

It appears both upgrade and BV are missing the closing } in the JSON, and you need a comma (,) between properties of the root object (i.e "upgrade" and "BV").

You also have differences is capitalization with what you delcared in your JSON and what you have in your:

choice(
    name: 'environment',
    choices: ['','Upgrade','BV' ],  <<<<<<<<< Upgrade vs upgrade!
    description: 'environment to choose'
    ),

])

and in your JSON:

{
  "upgrade": {    <<<< probably should make them match
  ...
}
  • Related