Tuesday, June 18, 2013

Text File to JSON with Talend Open Studio


If you have a text file, Excel file, or RDBMS table, it's easy to generate a JSON file with Talend Open Studio.  This JSON file can be put under a web server and accessed by JavaScript.
JSON is a formatted string used to exchange data.  Since it's text-based, it can be used by most languages and libraries like Dojo, jQuery, and PHP.  In many applications, server-side code like a PHP page will run a query and return a JSON string (rather than HTML) for use by a JavaScript library.  For example, a query of a list of services containing codes and descriptions will be used in a select-type UI control.

The format of JSON is simple. Like XML, data is mixed with additional information describing the data.  In a simple case, this JSON returns a two-item list.

{items=[
{Code: SWI, Description: "Swimming Pool"},
{Code: PET, Description: "Pets"}
]}


The various brackets and braces are easy to form, but if you have an input source like a text file or spreadsheet containing thousands of entries, it can be time consuming to covert this.

This is where Talend Open Studio can help.

Using a simple two-stage job, you can feed a text file into a special Talend component, tFileOutputJSON.  The text file contains two columns of data: Service_Code and Service_Description.

Service Code,Service Description
PET,Pets Allowed
SWI,Swimming Pool
TEN,Tennis Court
DRY,Dry Cleaning
INT,Internet Access
WIF,WIFI Internet Access
FIT,Fitness Room
CON,Concierge

Talend Excel to JSON with Excel File Properties Showing
Here's the same job with showing the Component details of the tFileOutputJSON component.

Talend Excel to JSON with JSON File Properties Showing

And here's the JSON output

{"data":[
{"Service_Description":"Pets Allowed","Service_Code":"PET"},
{"Service_Description":"Swimming Pool","Service_Code":"SWI"},
{"Service_Description":"Tennis Court","Service_Code":"TEN"},
{"Service_Description":"Dry Cleaning","Service_Code":"DRY"},
{"Service_Description":"Internet Access","Service_Code":"INT"},
{"Service_Description":"WIFI Internet Access","Service_Code":"WIF"},
{"Service_Description":"Fitness Room","Service_Code":"FIT"},
{"Service_Description":"Concierge","Service_Code":"CON"}
]}

Although most apps can produce JSON using server-side code, consider publishing data sets that don't change frequently using Talend.  HTML files servered up by a web server like Apache is extremely fast.  The files can be replicated all over the Internet too.  If you're dealing with a lot of reference data, infrequently refreshed data sets, or other lists, a few well-placed Talend jobs can get your Javascript developers coding.

No comments: