Skip to content

Instantly share code, notes, and snippets.

@JamoCA
Last active Nov 17, 2021
Embed
What would you like to do?
isJsonStructure(): A CFML user-defined function (UDF) that returns true if the string is a JSON-serialized struct or array. 2021-08-20
<cfscript>
// 2020-08-20 isJsonStructure(string, type=all);
// https://gist.github.com/JamoCA/e153c2ea40bfd75b60d180fbb709fe5b
public boolean function isJsonStructure(required string string, string type="any") output=false hint="I return true if the string is a JSON-serialized struct or array." {
if (not isJson(arguments.string)){
return false;
}
local.parsed = deserializeJson(arguments.string);
if (not local.keyExists("parsed")){
return false;
}
if (isStruct(local.parsed) and (not len(trim(arguments.type)) or listFindNoCase("any,struct,structure,object,record,dictionary,hashtable,hash table,keyed list,associative array", trim(arguments.type)))){
return true;
} else if (isArray(local.parsed) and (not len(trim(arguments.type)) or listFindNoCase("any,array,vector,list,sequence", trim(arguments.type)))){
return true;
}
return false;
}
</cfscript>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment