The other day I had the need to create a nested structure based off of a key using dot notation.

Below is the function I came up with:


private struct function convertStringToStruct(required string key, required any value, string delimiter = ".") {
    var obj = StructNew();
    var first = ListFirst(arguments.key, arguments.delimiter);
    var rest = ListRest(arguments.key, arguments.delimiter);

    if (Len(rest)) {
        obj[first] = convertStringToStruct(rest, arguments.value, arguments.delimiter);
    } else {
        obj[first] = arguments.value;

    return obj;


Here’s a quick example usage:


	// Declare a struct. We will later append to this
	obj = StructNew();
	// Create a few top level keys
	obj["ab"] = "foo";
	obj["b"] = "bar";
	// Append a dynamically created structure
	StructAppend(obj, convertStringToStruct("a.b.c", "baz"), false);
	// Show our output


The output from the above example:

cfdump output of convertStringToStruct() example

Here is a gist containing the code:


Tagged with: ColdFusion

blog comments powered by Disqus