Question

Please consider the code below:

I am getting JSON data based on the date range I am specifying on the url & I am deserializing it as folows:

<cfhttp url="#urladdress#" method="GET" resolveurl="Yes" throwonerror="Yes">

 <cfset cfData=DeserializeJSON(CFHTTP.FileContent)> 

<cfset arraylen = ArrayLen(cfdata)>

I am grabbing the data and inserting into MySQL database as follows:

<cfquery datasource="XX.X.XX.XXX" name="qCoulmnInsert">
    INSERT INTO Mytable
     (aol_int,
     android_Phone_int,
     androidTablet_int,
     apple_mail_int,
     blackberry_int,
     Eudora_int,
     gMail_int,
     Hotmail_int,
     lotus_notes_int,
     other_int,
     other_webmail_int,
     Outlook_int,
     Postbox_int,
     sparrow_int,
     thunderbird_int,
     windowsLiveMail_int,
     yahoo_int,
     iPad_int,
     iphone_int,
     iPod_int,
    <!--- FOR DATES --->
     startdate_dt,
     enddate_dt,
     date_dt)

   VALUES
     <!--- loop through your array --->
     <cfloop from="1" to="#arrayLen(cfData)#" index="i">
     ( <!--- 1 --->
     <cfif structKeyExists(cfData[i], "open")>
     <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["aol"]#">
     <cfelse>
     NULL
     </cfif>,

     <!--- 2 --->
     <cfif structKeyExists(cfData[i], "open")>
     <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["Android Phone"]#">
     <cfelse>
     NULL
     </cfif>,

     <!--- 3 --->
     <cfif structKeyExists(cfData[i], "open")>
     <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["Android Tablet"]#">
     <cfelse>
     NULL
     </cfif>,

     <!--- 4 --->
     <cfif structKeyExists(cfData[i], "open")>
     <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["Apple Mail"]#">
     <cfelse>
     NULL
     </cfif>,

     <!--- 5 --->
     <cfif structKeyExists(cfData[i], "open")>
     <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["Blackberry"]#">
     <cfelse>
     NULL
     </cfif>,

     <!--- 6 --->
     <cfif structKeyExists(cfData[i], "open")>
     <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["Eudora"]#">
     <cfelse>
     NULL
     </cfif>,

     <!--- 7 --->
     <cfif structKeyExists(cfData[i], "open")>
     <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["gmail"]#">
     <cfelse>
     NULL
     </cfif>,

     <!--- 8 --->
     <cfif structKeyExists(cfData[i], "open")>
     <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["hotmail"]#">
     <cfelse>
     NULL
     </cfif>,

     <!--- 9 --->
     <cfif structKeyExists(cfData[i], "open")>
     <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["Lotus Notes"]#">
     <cfelse>
     NULL
     </cfif>, 

     <!--- 10 --->
     <cfif structKeyExists(cfData[i], "open")>
     <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["Other"]#">
     <cfelse>
     NULL
     </cfif> ,

     <!--- 11 --->
     <cfif structKeyExists(cfData[i], "open")>
     <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["Other Webmail"]#">
     <cfelse>
     NULL
     </cfif>,

     <!--- 12 ---> 
     <cfif structKeyExists(cfData[i], "open")>
     <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["Outlook"]#">
     <cfelse>
     NULL
     </cfif> ,

     <!--- 13 --->
     <cfif structKeyExists(cfData[i], "open")>
     <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["Postbox"]#">
     <cfelse>
     NULL
     </cfif> ,

     <!--- 14 --->
     <cfif structKeyExists(cfData[i], "open")>
     <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["Thunderbird"]#">
     <cfelse>
     NULL
     </cfif> ,

     <!--- 15 --->
     <cfif structKeyExists(cfData[i], "open")>
     <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["Sparrow"]#">
     <cfelse>
     NULL
     </cfif> ,

     <!--- 16 --->
     <cfif structKeyExists(cfData[i], "open")>
     <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["Windows Live Mail"]#">
     <cfelse>
     NULL
     </cfif> ,

     <!--- 17 --->
     <cfif structKeyExists(cfData[i], "open")>
     <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["Yahoo"]#">
     <cfelse>
     NULL
     </cfif> ,

     <!--- 18 --->
     <cfif structKeyExists(cfData[i], "open")>
     <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["iPad"]#">
     <cfelse>
     NULL
     </cfif> ,

     <!--- 19 --->
     <cfif structKeyExists(cfData[i], "open")>
     <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["iPhone"]#">
     <cfelse>
     NULL
     </cfif> ,

     <!--- 20 --->
     <cfif structKeyExists(cfData[i], "open")>
     <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["iPod"]#">
     <cfelse>
     NULL
     </cfif> ,

  <!--- FOR DATES --->
     <cfif structKeyExists(cfData[i], "startdate_dt")>
     <cfqueryparam CFSQLTYPE="CF_SQL_DATE" value="#cfData[i].startdate_dt#">
     <cfelse>
     NULL
     </cfif>, 

     <cfif structKeyExists(cfData[i], "enddate_dt")>
     <cfqueryparam CFSQLTYPE="CF_SQL_DATE" value="#cfData[i].enddate_dt#">
     <cfelse>
     NULL
     </cfif>,

     <cfif structKeyExists(cfData[i], "date")>
     <cfqueryparam CFSQLTYPE="CF_SQL_DATE" value="#cfData[i].date#">
     <cfelse>
     NULL
     </cfif> 

    )

     <cfif i neq arrayLen(cfData)>,</cfif>
     </cfloop>
    </cfquery>

Problem I am facing:

When the JSON data does not has "Lotus Notes" in it, I get an error "Element Lotus Notes is undefined in a CFML structure referenced as part of an expression. "

an points to the line <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["Lotus Notes"]#">

When it has Lotus Notes in the JSON , it picks up some other element and I got the error "Element Eudora is undefined in a CFML structure referenced as part of an expression. " and points to the line <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["Eudora"]#"> in above code

Here is the sample JSON with Lotus Notes on date Jan 01

and here it is without Lotus Notes (from Jan 02) field where I get Eudora error

I don't understand where I am wrong. Please advise

Was it helpful?

Solution 2

I don't understand why you don't check before you reference. This is how I'd write it.

 <!--- 1 --->
 <cfset data = cfData[i]>
 <cfset isVarDefined = isDefined("variables.data.open.aol")>
 <cfset value = isVarDefined ? data.open.aol : "">
 <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#value#" null="#!isVarDefined#">

 <!--- 2 --->
 <!--- very similar to the code above... --->

OTHER TIPS

You only need a single check for whether cfData[i].Open exists - if it doesn't all those ints are going to be null, so just output 20 nulls. (Or perhaps don't bother with the insert at all - depends what's going on here).

Also, you don't need explicit hard-coded logic for every single one - just use a loop and check the existence inside the loop with a single piece of code - i.e:

<cfif StructKeyExists(cfData[i],'open')>

    <cfloop index="Key" array=#['aol','Android Phone','Android Tablet','etc']# >

        ,
        <cfif StructKeyExists( cfData[i].open , Key ) >
            <cfqueryparam value=#cfData[i].open[Key]# cfsqltype="cf_sql_integer" />
        <cfelse>
            <cfqueryparam value="" null=true />
        </cfif>

    </cfloop>

<cfelse>

    #RepeatString( ',null' , 20 )#

</cfif>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top