This got me to thinking about how much easier this can make querying and filtering data from a remote API request.
In this simple example we’ll make a request to the Twitter search API to return the last ten results for our search criteria, ‘ColdFusion’.
As we’re receiving JSON from the response, we can easily deserialize it into a format that we can then pass directly into the queryNew() method.
We instantly have an array of structures to send in to our query.
Saving The Data As A Query
Let’s jump straight to the code:
Assuming that we have records returned in the API request, we should now have a query object populated with the results directly from the remote call:
A Small But Important Caveat
We need to provide the column names for the queryNew() method. As you can see in the code sample above, we took the first result in the response and set the structure key names in to a list to serve this purpose.
For the majority of instances, depending on what API you call, this would probably suffice as the structure of the results wouldn’t change.
The Twitter API, however, will amend the result structure if a particular tweet is a reply by adding in some additional parameters. If these are encountered, the values of the query will not match up with the column names. Something to consider.
Now that we have this data, we can easily run a query of queries to further filter the results, or to manipulate them however we wish to:
ColdFusion already makes working with a JSON response easy. Having the ability to now convert data like this into a query is incredibly cool and helps to make it even easier!