Quantcast
Channel: Adobe Community : Popular Discussions - ColdFusion
Viewing all articles
Browse latest Browse all 14291

QueryToJson

$
0
0

Hi i want to convert Query object into json data::

 

But i got this type record:

[{"PRODUCTNAME":"2014 Kia Cadenza Premium 4dr Sedan (3.3L 6cyl 6A)","PRODUCTCATEGORYID":1,"PRODUCTID":1},{"PRODUCTNAME":"2014 Chevrolet Cruze Diesel 4dr Sedan (2.0L 4cyl Turbodiesel 6A)","PRODUCTCATEGORYID":1,"PRODUCTID":2}]


And I wan  to this type record::

[{"ProductName":"2014 Kia Cadenza Premium 4dr Sedan (3.3L 6cyl 6A)","ProductCategoryId":1,"ProductId":1},{"ProductName":"2014 Chevrolet Cruze Diesel 4dr Sedan (2.0L 4cyl Turbodiesel 6A)","ProductCategoryId":1,"ProductId":2}]

I want Field name according to my Table Column Name...(Not all characters of columns are capital like above)


my code:


QueryToJson.cfm

<!DOCTYPE html>

<html>

  <body>

<cfscript>

  myJSON=new Component.queryTojson().queryToJSON();

</cfscript>

  <cfdump var="#myJSON#" >

  </body>

</html>

 

queryTojson.cfc

<cfcomponent>

<cffunction name="queryToJSON" returntype="string" access="public" output="yes">

  <cfquery name="getallproducts" datasource="TestingDataSource">    

     SELECT TOP 2 * from Product;

    </cfquery>

  <cfset var o=ArrayNew(1)>

  <cfset var i=0>

  <cfset var r=0>

  <cfloop query="getallproducts">

    <cfset r=Currentrow>

    <cfloop index="i" list="#(getallproducts.columnList)#">

      <cfset o[r][i]=Evaluate(i)>

    </cfloop> 

  </cfloop>

    <cfreturn SerializeJSON(o)>

</cffunction>

</cfcomponent>

 

 

Product(Table)

ProductId

ProductCategoryId

ProductName

 



Viewing all articles
Browse latest Browse all 14291

Trending Articles