Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trycf/89a0722c5b5ee25c87302ac28324642c to your computer and use it in GitHub Desktop.
Save trycf/89a0722c5b5ee25c87302ac28324642c to your computer and use it in GitHub Desktop.
TryCF Gist
<cfscript>
function ConvertEmptyStringKeyValueToNull(data) {
if (isStruct(arguments.data)) {
for (local.key in arguments.data) {
arguments.data[local.key] = ConvertEmptyStringKeyValueToNull(arguments.data[local.key]);
}
} else if (isArray(arguments.data)) {
for (local.i = 1; local.i <= arrayLen(arguments.data); local.i++) {
arguments.data[local.i] = ConvertEmptyStringKeyValueToNull(arguments.data[local.i]);
}
} else if (arguments.data eq "") {
arguments.data = javaCast("null", "");
writedump(arguments);abort;
}
return arguments.data;
}
</cfscript>
<!-- Sample data -->
<cfset data = {
name = "",
age = 25,
address = {
street = "",
city = "New York",
zip = ""
},
contacts = [
{ type = "email", value = "" },
{ type = "phone", value = "123-456-7890" }
]
}>
<!-- Convert empty strings to null -->
<cfset updatedData = ConvertEmptyStringKeyValueToNull(data)>
<!-- Display the updated data -->
<cfdump var="#updatedData#">
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment