Alfresco REST API, how to generate node properties JSON Object in Groovy

To be able to create a node, or update it, using the Alfresco REST API you will need to send the node’s properties in a JSON format along with the request.

If you are doing this programmatically in Groovy (let’s say, from an APS task script) you’ll find very useful the following code snippet.

/**
 * Gets node's properties in JSON format
 * @param type node content type
 * @param propertyMap node properties map
 * @param aspectList node aspect list
 * @return properties json object
 */
static String getPropertiesJson(String type, Map<String, String> propertyMap, List<String> aspectList) {
    def builder = new JsonBuilder()
    builder {
        name(propertyMap.get("cm:name"))
        nodeType(type)
        aspectNames(aspectList.collect { oneAspect -&gt; oneAspect })
        properties {
            propertyMap.each { key, value -&gt;
                ["${key}"(value)]
            }
        }
    }
 
    println(builder.toPrettyString())
    return builder.toString()
}