//Using the fixed values in below for demonstration only. Assign the data from variables / properties as needed
//Assign the response dynamically from previous step
def jsonString = '' '[
{
"SourceId": "bc3cef1e-a9f1-46df-a4f0-c1131357ea57",
"SourceCode": "ABC",
"CodeTier": "123",
"SourceDescription": "ABC Desc"
},
{
"SourceId": "b4035134-ca33-4b33-b3c7-06ea880f1f28",
"SourceCode": "DEF",
"CodeTier": "456",
"SourceDescription": "DEF Descr"
},
{
"SourceId": "9666bd19-1916-4052-9044-06b0e38e9175",
"SourceCode": "GHI",
"SourceDescription": "GHI Descr"
}
]' ''
def json = new groovy.json .JsonSlurper ( ) .parseText ( jsonString)
def getSourceIdBySourceCode = { code -> json.find { it? .SourceCode == code} ? .SourceId }
println "SourceId for ABC is : ${getSourceIdBySourceCode('ABC')}"
//Test all the source id and codes
def expectedCodeIdMap = [ ABC: 'bc3cef1e-a9f1-46df-a4f0-c1131357ea57' , DEF: 'b4035134-ca33-4b33-b3c7-06ea880f1f28' , GHI: '9666bd19-1916-4052-9044-06b0e38e9175' ]
expectedCodeIdMap.each { code, id ->
println "verifying $code with expected $id"
assert id
== getSourceIdBySourceCode
( code
) }
println getSourceIdBySourceCode( '123' )
Ly9Vc2luZyB0aGUgZml4ZWQgdmFsdWVzIGluIGJlbG93IGZvciBkZW1vbnN0cmF0aW9uIG9ubHkuIEFzc2lnbiB0aGUgZGF0YSBmcm9tIHZhcmlhYmxlcyAvIHByb3BlcnRpZXMgYXMgbmVlZGVkCgovL0Fzc2lnbiB0aGUgcmVzcG9uc2UgZHluYW1pY2FsbHkgZnJvbSBwcmV2aW91cyBzdGVwCmRlZiBqc29uU3RyaW5nID0gJycnWwogICAgewogICAgICAgICJTb3VyY2VJZCI6ICJiYzNjZWYxZS1hOWYxLTQ2ZGYtYTRmMC1jMTEzMTM1N2VhNTciLAogICAgICAgICJTb3VyY2VDb2RlIjogIkFCQyIsCiAgICAgICAgIkNvZGVUaWVyIjogIjEyMyIsCiAgICAgICAgIlNvdXJjZURlc2NyaXB0aW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