Solution: How to assign JSON Maps to Jython Key Value Maps and add to a List

Sometimes, our plugins or direct API calls give back some sort of JSON like in this example for AzureDevops containing some more complex data to be mapped to internal releaseVariable types:
preconditions:

  1. data contains some JSON structure like this:

    [
    {
    “azBoards”:{
    “taskNumber”:“5958”,
    “pbiNumber”:“5959”
    },
    “projectName”:“DevSecOps Playground”,
    “buildDefinitionName”:“Main-CI Pipeline”,
    “releaseDefinitionName”:"[Quali][ART] Production release pipeline"
    },
    {
    “azBoards”:{
    “taskNumber”:“5960”,
    “pbiNumber”:“5961”
    },
    “projectName”:“DevSecOps Playground”,
    “buildDefinitionName”:“Main-CI Pipeline”,
    “releaseDefinitionName”:"[Quali][QMSA] Production release pipeline"
    },
    {
    “azBoards”:{
    “taskNumber”:“5962”,
    “pbiNumber”:“5963”
    },
    “projectName”:“DevSecOps Playground”,
    “buildDefinitionName”:“Main-CI Pipeline”,
    “releaseDefinitionName”:"[Quali][ATP] Production release pipeline"
    },
    {
    “azBoards”:{
    “taskNumber”:“5964”,
    “pbiNumber”:“5965”
    },
    “projectName”:“DevSecOps Playground”,
    “buildDefinitionName”:“Main-CI Pipeline”,
    “releaseDefinitionName”:"[Quali][CSNV_RMA] Production release pipeline"
    }
    ]

  2. releaseVariable[‘jsonString’] is defined as Text type(which is a string Var) in XLR template/release variable section.

  3. releaseVariable[‘azBoardsList’] is defined as List Type in template/release var section

  4. releaseVariable[‘azBoard’] is defined as Key Value Map Type in template/release var section

  5. Jython code as follows
    import json

     #get somehow the data from API or Rest Calls  
     data = json.loads(data)
    
     releaseVariables['jsonString'] = data
      
     for rec in data:
             print("Project Name : %s" % rec['projectName'])
             releaseVariables['projectName'] = rec['projectName']
             releaseVariables['buildDefinitionName'] = rec['buildDefinitionName']
             releaseVariables['releaseDefinitionName'] = rec['releaseDefinitionName']
             releaseVariables['azBoard']['taskNumber'] = rec['azBoards']['taskNumber']
             releaseVariables['azBoard']['pbiNumber'] = rec['azBoards']['pbiNumber']
             releaseVariables['azBoardsList'].append(releaseVariables['azBoard'])