Reputation: 1009
is there any way to return a real JSON object through a function in ColdFusion? My current solution is to turn a query into a string and in another CF-file I'm converting it back to a JSON object:
<cffunction name="addLicense" access="remote" returntype="string" returnFormat="JSON" httpmethod="POST">
<cfquery datasource="hostmanager" name="createCustomer">
SELECT * FROM license
</cfquery>
<cfreturn serializeJSON(createCustomer)>
</cffunction>
Upvotes: 3
Views: 969
Reputation: 1009
Now I'm using this solution to return JSON:
returntype="any" produces="application/json"
Upvotes: 2
Reputation: 116
You could use returnType="query" and the function will return the results in JSON format. You might need to use <cfcontent type="application/json">
to make sure the returned content type is correct.
Upvotes: 1
Reputation: 11120
There are many different ways to use serializeJSON
<cfscript>
myQuery = queryNew("id,name,amount","Integer,Varchar,Integer",
[
{id=1,name="One",amount=15},
{id=2,name="Two",amount=18},
{id=3,name="Three",amount=32}
]);
writeOutput("The new query is:")
writeDump(myQuery)
</cfscript>
<cfoutput>
<h4>Default</h4>
<p><code>#serializeJSON(myQuery)#</code></p>
<h4>Row</h4>
<p><code>#serializeJSON(myQuery, "row")#</code></p>
<h4>Column</h4>
<p><code>#serializeJSON(myQuery, "column")#</code></p>
<h4>Struct</h4>
<p><code>#serializeJSON(myQuery, "struct")#</code></p>
</cfoutput>
Which Results in
Upvotes: 5
Reputation: 15846
In ColdFusion2016 I use something like this.
deserializeJSON(serializeJSON(createCustomer, 'struct'))
This basically give you an array of structures(with query columns as struct keys).
Upvotes: 3