英文:
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": {
...
}
}
看起来upgrade
和BV
两者在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:
{
"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
...
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论