I have a list that looks like this - [1,2,3,4,5,6]
I have an array of JSON objects that looks like this -
Full List: [{ "Key": "CI", "Value": "SERVICES" } , { "Key": "CI", "Value": "" }]
Expected output -
Full List: [{ "Key": "CI", "Value": "SERVICES", "Account": "1" } , { "Key": "CI", "Value": "", "Account": "2" }]
How to do this inside jenkins file using groovy. I have tried and failed.
This should get you started:
def list = [1,2,3]
def full_list = ['{ "Key": "CI", "Value": "SERVICES" }' , '{ "Key": "CI", "Value": "" }']
full_list.eachWithIndex { item, index ->
def json = new groovy.json.JsonSlurper().parseText(item)
json["Account"] = list[index]
full_list[index] = json
}
Output:
["[Account:1, Key:CI, Value:SERVICES]", "[Account:2, Key:CI, Value:]"]
https://onecompiler.com/groovy/3zddfxgan
We simply loop through the array, convert each item to a json map object, append the new "Account" key into the map with the corresponding value of the other list, and replace the original array item with the modified map value.