在Groovy流水线中调用JSON中的每个键和值。

huangapple go评论56阅读模式
英文:

Calling each key and value in json in groovy pipeline

问题

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

println list['level']
println list['domain']
println list['resources']
println list['resources']['db-schemas']
println list['resources']['db-schemas']['name']
println list['resources']['db-schemas']['name']
println list['resources']['db-schemas']['name']
println list['resources']['db-schemas']['name']['jvm']
println list['jvm']
println list['jvm']['type']
println list['jvm']['type']['(.jvm-name)']
println list['jvm']['type']['(.jvm-name)']['jenkins-name']
println list['jvm']['type']['(.jvm-name)']['host']
println list['jvm']['type']['(.jvm-name)']['ip']
println list['jvm']['type']['(.jvm-name)']['port']
英文:

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

         
     
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)

答案1

得分: 1

这不是一个Groovy问题。您的JSON存在语法错误。缺少一个}来关闭"upgrade"对象。所以这是您发布的内容:

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

应该是这样的:

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

看起来upgradeBV两者在JSON中都缺少了闭合的},并且根对象(即"upgrade"和"BV")的属性之间需要逗号(,)。

您还注意到大小写在您的JSON和您的代码中有所不同:

在您的代码中:

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

和您的JSON中:

{
  "upgrade": {    <<<<<<<< 可能应该使它们匹配
  ...
}
英文:

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:

{
    &quot;upgrade&quot;: {
     ...
    &quot;BV&quot;: {
     ...
}

What it should be is:

{
    &quot;upgrade&quot;: {
    ...
    },
    &quot;BV&quot;: {
    ...
    }
}

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: &#39;environment&#39;,
    choices: [&#39;&#39;,&#39;Upgrade&#39;,&#39;BV&#39; ],  &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt; Upgrade vs upgrade!
    description: &#39;environment to choose&#39;
    ),

])

and in your JSON:

{
  &quot;upgrade&quot;: {    &lt;&lt;&lt;&lt; probably should make them match
  ...
}

huangapple
  • 本文由 发表于 2023年1月9日 06:25:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75051695.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定