Working with Object DataSources

Working with JSON Files

Object DataSource supports not only JAVA files, but also JSON files. There is an example file named Amount.json. Here is how to read data from this file using the Object DataSource.

  1. Review the sample file Amount.json, as shown in Figure 10.3, “Sample JSON file”. Remember the location of the JSON file as we will need to reference it.

    Figure 10.3. Sample JSON file

    Sample JSON file
  2. Create a new Object DataSource called Object2. Click Add. The Add Column dialog opens.

  3. Fill in column names and data types according to those written in the JSON file. They must be exactly the same, so that data can be successfully loaded.

  4. Click Next. In the JavaScript tab page, fill in the following code:

    function pushTo(/*PushContext*/ cxt, /*DataListener*/ dl)
    {
      dl.startData(this);
      var contents = this.getRepositoryFileContents("FilePath/
      Amount.json");
      if (contents!=null) {
        var barley = eval(""+contents);
        var len = barley.length;
        for (var i=0;i<len;i++)
        {
          var rec = this.buildRecord(barley[i]);
          dl.processRecord(rec);
        }
      }
      dl.endData(this);
    }
  5. Click Finish. The Object datasource is added to the repository.

  6. Click Load Data in the Data window. View the data to verify if the correct data is returned, as shown in Figure 10.4, “Load data from JSON file”.

    Figure 10.4. Load data from JSON file

    Load data from JSON file