Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Scriptrunner Groovy assistance - convert list to JSON

Véronique
Contributor
January 25, 2023

Hi,

Some context:

To automate builds in Nutanix CALM, from within a jira workflow transition, we have to perform some API calls. With call A, we retreive some variables

FYI: call A: https://server.domain.com:9440/api/nutanix/v3/blueprints/ which allows us to retreive variable "deployment_create_list" amongst others.

if(cont){
    try{
        br = Utils.get(postCalmBluePrintApplicationProfileUrl+bluePrintUuid, authStr);
        while ((input = br.readLine())!=null){
            
            def jsonSlurper = new JsonSlurper();
            def obj = jsonSlurper.parseText(input);
            log.error("INFO "+obj.status.resources.app_profile_list[0].uuid);
            appProfileUuid = obj.status.resources.app_profile_list[0].uuid;
            
            for(def variable in obj.status.resources.app_profile_list[0].variable_list){
                mappingVariables[variable.name]=variable.uuid;
            }
            
            SDLName = obj.spec.resources.substrate_definition_list[0].name
            SDLUUID = obj.spec.resources.substrate_definition_list[0].uuid
            projectUuid = obj.metadata.project_reference.uuid
            deployment_in_bp = obj.spec.resources.app_profile_list[0].deployment_create_list; 
        }
        br.close();

With call B, we start the automated build in Nutanix CALM. Parameter deployment_create_list is required.

Call B: https://server.domain.com:9440/api/nutanix/v3/blueprints/.

Issue:

The value that we retreive from the first call for deployment_create_list is a list, not JSON format:

[
    [uuid:e12c83cc-bb59-4431-bafb-4c23442734bc, name:e64c0010_deployment, action_list: [], package_local_reference_list: [
            [kind:app_package, name:Package1, uuid:c096e8f0-df68-484b-8537-b53553bcff23
            ]
        ], default_replicas: 1, depends_on_list: [], min_replicas: 1, published_service_local_reference_list: [], max_replicas: 1, substrate_local_reference: [kind:app_substrate, name:Windows, uuid: 5339883c-ddfa-4c8d-9046-e806d7563a94
        ], type:GREENFIELD, variable_list: [], description:
    ],
    [uuid:a918774b-ae59-4e0b-894a-09d49a0bf3c9, name:e1c6e997_deployment, action_list: [], package_local_reference_list: [
            [kind:app_package, name:Package2, uuid: 773890ad-ad1c-4ac8-a1e7-74ff26db7ec3
            ]
        ], default_replicas: 1, depends_on_list: [], min_replicas: 1, published_service_local_reference_list: [], max_replicas: 1, substrate_local_reference: [kind:app_substrate, name:Ansible MGMT VM, uuid:f61dd8f9-d9ba-407d-9f86-401a176f26c7
        ], type:GREENFIELD, variable_list: [], description:
    ]
]
Call B requires this value to be in JSON format. 
Anyone an idea on how we can transform this value in the most simple way to JSON format? Looks like now we have to iterate through it to "manually" convert it to JSON.
Thank you in advance!

1 answer

1 accepted

0 votes
Answer accepted
Ram Kumar Aravindakshan _Adaptavist_
Community Champion
January 25, 2023

Hi @Véronique

For your requirement, you will need to use the JsonOutput object to convert the List of Maps into a JSON.

You could try something like:-

import groovy.json.JsonOutput

def list = [[field:
"A", amount:1000], [field:"A", amount:2000], [field:"B", amount:3000], [field:"C", amount:4000]]


def json  = JsonOutput.toJson(list.groupBy{it.field}.collect{['field':it.key, 'amount':it.value.sum{it.amount}]})

log.warn "Output: ${json}"

Please note that the sample code above is not 100% exact to your environment. Hence, you will need to make the required modifications.

I hope this helps to solve your question.

Thank you and Kind regards

Ram

Véronique
Contributor
January 25, 2023

Thank you for your useful reply! It has guided us to the solution :).

 

 deployments_in_bp = groovy.json.JsonOutput.toJson(obj.spec.resources.app_profile_list[0].deployment_create_list); 

 

Have a great day. 

Mia Paulin
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 20, 2023

@Véronique You are awesome!

Suggest an answer

Log in or Sign up to answer