Binding JSON Data to a Clarify control using ClearBasic
A while back, I posted some information on how to consume JSON data in ClearBasic.
Someone recently asked me if you could bind a set of data from a web service into a Clarify grid. Of course. Similar idea to what I posted earlier, just now we want to take the object that we created from the JSON, and turn it into something that the Clarify UI can consume. For a Clarify grid, a list of records will work.
I created a simple web service that returned a collection of data in JSON format. To be honest, I used the Dovetail SDK to query for a list of the user’s open cases, and return it in JSON format. But the web service could return any data, from any source.
Here’s the basic code:
Transpose a JavaScript Object Into a List Of Records
The interesting function in there is TransposeJavaScriptObjectIntoListOfRecords. This takes a JavaScript object, and turns it into a list of records.
What’s happening here?
We loop through the results from the web service. For each one, we create a new record, and add that record to a list. Then we return the list of records
Notice that this function takes two lists – one is a list of properties from the javascript object, and one is a list of fields in the record. This allows the caller to say Put the data from this property of the JS object into that field of the record. Pretty simple.
Fill the Cobj
Now that we have a list of records, we simply fill that list of records into a contextual object, as shown in the first code snippet.
Filled Clarify Grid
And our form renders the data:
Code
The complete form code is on github: https://gist.github.com/925692
Summary
Now that you know how to consume JSON from ClearBasic, and bind it to Clarify UI controls, you can consume data from all sorts of sources, including many web services, and have it render inside of the Clarify Client.
Rock on.