Created
April 19, 2024 06:16
-
-
Save trycf/89a0722c5b5ee25c87302ac28324642c to your computer and use it in GitHub Desktop.
TryCF Gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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