0

I am passing a CFC a JSON, and there is a key that may or may not exist. So, I am checking it with isDefined, but for whatever reason, coldfusion doesn't think it exists, when, from what I'm seeing it clearly does. I have tried everything I can think of, but now I'm at a loss for what the problem is.

Here is the JSON pulled from Firebug:

[{"type":"USEQUIT","date":"01/01/2011"}] //the date, may or may not exist

Here is the only important part of the CFC function:

<cfargument name="attribs">
//debugging code
<cfif isDefined("attribs.date")>
    <cfdump var="date here">
<cfelse>
    <cfdump var="date not here">
</cfif>

I do this above in other function, and it works fine, but for whatever reason, I cannot get it to work here. I'm assuming that I'm doing something wrong, but I cannot figure it out, so I'm asking the community.

Any thoughts?

Rob M
  • 927
  • 2
  • 14
  • 33

1 Answers1

3

CF sees the json you pass as plain text string until you call deselializeJSON() on it:

<cfargument name="attribs">
//debugging code
<cfset var ds_attribs = deserializeJSON(arguments.attribs)>
<cfif structKeyExists(ds_attribs[arrayLen(ds_attribs)], "date")>
    <cfdump var="date here">
<cfelse>
    <cfdump var="date not here">
</cfif>
Rob M
  • 927
  • 2
  • 14
  • 33
azawaza
  • 2,940
  • 1
  • 14
  • 19
  • I was pretty close to that prior to you answering it. The problem with your code is that it throws an error as written. `You have attempted to dereference a scalar variable of type class coldfusion.runtime.Array as a structure with members.` I added this and it worked. `` and it worked. – Rob M May 31 '12 at 15:29