Reputation: 449
i have a json file like the below one..
{
"env1":{
"region":{
"region1":{
"var1":"test"
},
"region2":{
"var1":"test"
},
"region3":{
"var1":"test"
}
}
},
"env2":{
"region":{
"region1":{
"var1":"test"
},
"region2":{
"var1":"test"
},
"region3":{
"var1":"test"
}
}
}
}
config_all = readJSON file: "${env.WORKSPACE}/<above-json-name>.json"
my concern is how do i dynamically get the config for different region at runtime.. i want to do something like this based on a variable..
def region = env.getProperty("region")
config = config_all.env2.${region} << something like this.. but i cannot aceive it..
is there a way to aceive this sort of dynamicity in the varilable value assignment in jenkins groovy.. thanks in advance
Upvotes: 0
Views: 303
Reputation: 28739
If you want to access values in a map with dynamic/variable keys, then you cannot use the object syntax, but rather must use the native syntax for accessing values from keys in a Map with [<key>]
.
Following this, we update your code snippet like:
config = config_all['env2'][region]
However, based on your JSON, that is probably not going to traverse your data correctly since it is missing the top-level region
key and the var1
key. In that case, it would more likely need to be:
config = config_all['env2']['region'][region]['var1']
That will assign the value test
to your variable config
.
Upvotes: 1