Use JSON as a Scripted Data Set

I was doing some work with JSON this weekend and I thought it might be fun to build a scripted data set from a JSON…

OpenText profile picture

OpenText

April 2, 20133 minutes read

Descriptive text explaining the contents of the image.
images

I was doing some work with JSON this weekend and I thought it might be fun to build a scripted data set from a JSON file. With just a few lines of JavaScript I had it up and running without much work.The first thing you will need is some JSON data to work with. Here’s the sample data I created and saved to my desktop as test.json

{"bindings":[{"name":"Bob","title":"Manager","phone":"555-000-0012"},{"name":"Tom","title":"Sales Rep","phone":"555-000-0037"},{"name":"Larry","title":"Tech","phone":"555-000-0021"}]}

Once you have the JSON data saved open the designer and create a new report. Then you need to create a scripted data source. From the data explorer right click “Data Sources” > “New Data Source” > “Scripted Data Source” > “Finish”

Now we’re going to need a scripted data set to work with. Right click “Data Sets” > “New Data Set” > Select the data source you just created > “Next”. From here we need to add three output columns named “name”, “title”, and “phone”. Then click “finish”.

Attached Image

Now that the output columns have been created we need to read the JSON file from open().

First we need to import java.io and org.apache.commons.io. If you don’t have the apache commons library it can befound on their website.

importPackage(Packages.org.apache.commons.io);
importPackage(Packages.java.io);

Next we’ll use the apache commons library to open the JSON file and store it in a String.

// Grab the JSON file and place it in a string
fisTargetFile =newFileInputStream(newFile("C:/Users/kclark/Desktop/test.json"));
input =IOUtils.toString(fisTargetFile,"UTF-8");// Store the contents in a variable
jsonData = input;

After we have the contents of the JSON file we need to convert it to a JSON object.

// Convert the String to a JSON object
myJSONObject =eval('('+ jsonData +')');

Then we need to get the length of the object because we will be accessing the data similar to how we use arrays in JavaScript.

// Get the length of the object
len = myJSONObject.bindings.length;

count =0;// Counter used to step through each item in the JSON object.

Finally our last step is to look through the JSON object and assign the appropriate data to the output columns from fetch().

// Loop through the JSON object adding it to the scripted data sourceif(count < len){var name     = myJSONObject.bindings[count].name;var title    = myJSONObject.bindings[count].title;var phone    = myJSONObject.bindings[count].phone;
  
  row["name"]= name;
  row["title"]= title;
  row["phone"]= phone;
  count++;returntrue;}returnfalse;

After this is done you can save the rptdesign, right click the data set > “edit” > “Preview Results”. If successful it will look like this.

Attached Image

Once complete you can now use this data set like any other, creating tables, charts, and other report items.

The complete example can be downloaded from this devshare. If you have any questions or comments please leave them below.

Share this post

Share this post to x. Share to linkedin. Mail to
OpenText avatar image

OpenText

OpenText, The Information Company, enables organizations to gain insight through market-leading information management solutions, powered by OpenText Cloud Editions.

See all posts

More from the author

Manutan combines digital services with the human touch to delight customers

Manutan combines digital services with the human touch to delight customers

At Manutan, we equip businesses and communities with the products and services they require to succeed. Headquartered in France, our company has three divisions, serving…

January 31, 2024 4 minutes read
Reaching new markets in Europe and beyond

Reaching new markets in Europe and beyond

How information management specialists at One Fox slashed time to market for innovative products with OpenText Cloud Platform Services At One Fox, we’ve driven some…

January 18, 2024 4 minutes read
SoluSoft helps government agencies tackle fraud faster

SoluSoft helps government agencies tackle fraud faster

Fraud, in all its forms, is a pervasive problem, spanning industries and preying on vulnerabilities in federal and state government systems. Each year in the…

November 21, 2023 3 minutes read

Stay in the loop!

Get our most popular content delivered monthly to your inbox.