Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A ColdFusion function to convert dot notation strings to structs
<cfscript>
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;
}
</cfscript>
<cfscript>
// 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
WriteDump(obj);
</cfscript>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment