'How to place "{}" as default value using StringSubstitutor in Java

While putting syntax like ${var_name:-{}} for a variable ,it returns output as variable value along with extra "}" or if variable value is null then it returns like {}} .. How to get rid of this extra "}"enter image description here


        ClassLoader currentCls=Thread.currentThread().getContextClassLoader();
        
        InputStream template = currentCls.getResourceAsStream("system/CompartmentTerraformTemplate.json");
        ObjectMapper _mapper = new ObjectMapper();
        JsonNode obj=_mapper.readTree(template);
        String templateString = obj.toString();
        
        InputStream data=currentCls.getResourceAsStream("system/RequestSample.json");
        HashMap<String,Object> mapRec=_mapper.readValue(data, HashMap.class);
        
        StringSubstitutor sub = new StringSubstitutor(mapRec);
        
        String finalString = sub.replace(templateString);
        finalString=finalString.replace("=", ":");
        System.out.println(finalString);        
    }

sample record for map
{
    "compartment_id":"ocid1.compartment.oc1..adgnljndfgvbcoasdbffbvovafeooves34r3",
    "description":"Testing for handling the JSON TF configuration",
    "name":"compt_abhi",
    "defined_tags":{"new":"users","Toy":"story"}
}

sample String Template in which value should be replaced

{
    "resource":{
        "oci_identity_compartment":{
            "compt_req":{
                "compartment_id":"${compartment_id}",
                "description":"${description}",
                "name":"${name}",
                "defined_tags":"${defined_tags:-{}}",
                "freeform_tags":"${freeform_tags:-{}}"
            }
        }
    }
}




Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source