Denoteone
Denoteone

Reputation: 4055

How to Invoke JSON data as the correct Data Type?

Question: What data type do I use when invoking JSON data from a database using ColdFusion?

Background: My application needs to pull some JSON data from a database and parse the data into an HTML form.

Below is my code so far:

<cftry>
    <cfinvoke component="UserData.cfc.data" method="editData" returnvariable="editReturn">
            <cfinvokeargument name="formID" value="#URL.dataID#">
    </cfinvoke> 

    <cfset ReBuild = DeserializeJSON(#editReturns#)>
<cfcatch type="Any">
        <cfoutput>
            <hr>
            <h1>Other Error: #cfcatch.Type#</h1>
            <ul>
                <li><b>Message:</b> #cfcatch.Message#
                <li><b>Detail:</b> #cfcatch.Detail#
            </ul>
        </cfoutput>
        <cfset errorCaught = "General Exception">
    </cfcatch>

</cftry>

UserData.cfc.data:

    <cffunction name="editData" access="public" returntype="any">


        <cfargument name="formID" required="yes">


        <!--- Select --->
            <cfquery name="UserData" datasource="RC">
                SELECT      Data
                FROM        USER_Forms
        WHERE       ID = <cfqueryparam value="#ARGUMENTS.formID#">
            </cfquery>
<!-- The information pulled from the database should be a Serialized JSON data. -->
        <cfreturn UserData>

    </cffunction>

Error Message:

    Other Error: Expression

Message: Complex object types cannot be converted to simple values.
Detail: The expression has requested a variable or an intermediate expression result as a simple value. However, the result cannot be converted to a simple value. Simple values are strings, numbers, boolean values, and date/time values. Queries, arrays, and COM objects are examples of complex values.
The most likely cause of the error is that you tried to use a complex value as a simple one. For example, you tried to use a query variable in a cfif tag.

When I added the data to the database I used the following process:

<cfset ForDBInsert = SerializeJSON(formCopy)>
<!-- Then I INSERTED ForDBInsert into the database columnn.  -->

Upvotes: 1

Views: 139

Answers (1)

Scott Stroz
Scott Stroz

Reputation: 7519

Try DeserializeJSON(editReturns.data) - notice I took out the # they are not needed when passing arguments this way. Looks like you are trying to deserialize the entire query object rather than the string itself.

Upvotes: 2

Related Questions