ColdFusion: how to check whether JSON property is null? -


let's post simple json payload:

{"foo":null} 

on coldfusion server, how check if 'foo' property null?

isdefined won't work because false null values. isnull won't work because isnull true not null values, missing properties.

<cfset json = deserializejson(gethttprequestdata().content) /> <cfdump var="#isdefined("json.foo")#" /> <!--- false ---> <cfdump var="#isnull(json.foo)#" /> <!--- true ---> <cfdump var="#isnull(json.bar)#" /> <!--- true ---> 

my mistake, thought null in json deserialized empty string, it's not true.

null in json translated struct key foo undefined in cf10. (not sure older cf version)

enter image description here

therefore, true isstructvaluenull() can written this:

function isstructvaluenull(struct, key) {     return listfind(structkeylist(struct), key)               && !structkeyexists(struct, key); }  json = deserializejson('{"foo":null,"bar":123}');  writedump(isstructvaluenull(json, "foo"));    // yes writedump(isstructvaluenull(json, "bar"));    // no 

or can loop through json , use structkeyexists(), if it's false, it's null.

function structnullkeylist(struct) {     var nulls = "";     (var key in struct)         if (!structkeyexists(struct, key))          nulls = listappend(nulls, key);     return nulls; }  writedump(structnullkeylist(json));           // 'foo' 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -