Skip to main content
Inspiring
December 18, 2017
Answered

SerializeJSON for key value pairs?

  • December 18, 2017
  • 1 reply
  • 813 views

I've built a functional REST service that is successfully returning a query result:

<cffunction name="getAllOrders" access="remote" returntype="any" httpmethod="GET" returnFormat="json">

   <cfquery name="getAllOrders" datasource="test">

      select * from concreteOrder

   </cfquery>

   <cfset results = #serializeJSON(getAllOrders)#>

   <cfreturn results>

</cffunction>

I've been trying different formats but I'm not understanding the docs (my apologies, the holidays has made my brain fuzzy).

Does anyone know the correct format I should use in the serializeJSON() method to successfully bring back name:value pairs from the query results in my JSON object? All I've been able to do is bring back each row as a blob and then my Angular app can't do anything with that.... Ideally I'd like to bring back each row with something like "id": "1", "col2":"value", "col3":"value".... and so on

    This topic has been closed for replies.
    Correct answer richardELeach

    ugh, I just got it sorted out....

    The docs are pretty in depth and here's what you need to do:

    my cffunction code is fine, leaving it exactly as is all I did was add this line to my Application.cfc and problem solved:

    <cfset this.serialization.serializeQueryAs='struct'/>

    Saved, reloaded the REST service in the CF Admin and the new correct format works just fine.

    I hope this helps someone else!

    1 reply

    richardELeachAuthorCorrect answer
    Inspiring
    December 18, 2017

    ugh, I just got it sorted out....

    The docs are pretty in depth and here's what you need to do:

    my cffunction code is fine, leaving it exactly as is all I did was add this line to my Application.cfc and problem solved:

    <cfset this.serialization.serializeQueryAs='struct'/>

    Saved, reloaded the REST service in the CF Admin and the new correct format works just fine.

    I hope this helps someone else!