cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Did you get called away in the middle of writing a post? Don't worry you can find your unfinished post later in the Drafts section of your profile page. X

FromJSON() returns empty infotable

jdurand
1-Newbie

FromJSON() returns empty infotable

Hi,

I have a JSON which is NOT empty and I want to convert it to infotable with FromJSON() but it returns me an empty infotable.

Here a part of my code :

var jsont = { dataShape: { fieldDefinitions : {} }, rows: [] };

    var cal1;

 

    if (type == "Temperature") {

    jsont.dataShape.fieldDefinitions['Temperature'] = { name: 'Temperature', baseType: 'NUMBER' };

    }

    else if (type == "EFS") {

        jsont.dataShape.fieldDefinitions['Mesure1'] = { name: 'Mesure1', baseType: 'NUMBER' };

        jsont.dataShape.fieldDefinitions['Mesure2'] = { name: 'Mesure2', baseType: 'NUMBER' };

    }

    /**if (row.Batterie) {

            result.dataShape.fieldDefinitions['Batterie'] = { name: 'Batterie', baseType: 'NUMBER' };

    }**/

  jsont.dataShape.fieldDefinitions['Timestamp'] = { name: 'Timestamp', baseType: 'DATETIME' };

 

    for (var x = 0; x < tableLength; x++) {

        var row = datas.rows;

        // CustomPropertiesHistory entry object

        newEntry = new Object();

     

        if (type == "Temperature") {

            newEntry.Temperature = 0;

            newEntry.Temperature = row.Temperature; // NUMBER

        }

        else if (type == "EFS") {

            //cal1 = row.Mesure1 * 10 * 0.001;

            cal1 = row.Mesure1 / 10;

            newEntry.Mesure1 = 0;

            if (cal1) {

            newEntry.Mesure1 = cal1; // NUMBER

            }

            newEntry.Mesure2 = 0;

        }

     

        if (row.Batterie) {         

            newEntry.Batterie = 0;

            newEntry.Batterie = row.Batterie; // NUMBER

        }

        newEntry.Timestamp = row.timestamp; // DATETIME - isPrimaryKey = true

        //newEntry.Timestamp = dateAddMilliseconds(row.timestamp, -3600000); // DATETIME - isPrimaryKey = true

        jsont = newEntry;

    }

 

    logger.error(jsont[3].Temperature);

    //var result = jsont;

    var params = {

  json: jsont /* JSON */

  };

  // result: INFOTABLE

  var result = Resources["InfoTableFunctions"].FromJSON(params);

    logger.error(result[3].Temperature);

At line 48 logger returns me the value but at line 59 it returns me an error :

Wrapped java.lang.IndexOutOfBoundsException: Index: 3, Size: 0 Cause: Index: 3, Size: 0

I also noticed that if I end my script with result containing the JSON, so var result = datashape ... Put new entries in result, ... logger returns the value asked but if I test my service, the table will appear empty.

1 ACCEPTED SOLUTION

Accepted Solutions
jamesm1
5-Regular Member
(To:jdurand)

Can you please post the JSON you are trying to convert to an InfoTable?

Edit: Actually, looking at this, I made a few changes to your code:

var jsont = { dataShape: { fieldDefinitions : {} }, rows: [] }; 

jsont.rows = [];

 

    var cal1; 

    

    if (type == "Temperature") { 

    jsont.dataShape.fieldDefinitions['Temperature'] = { name: 'Temperature', baseType: 'NUMBER' }; 

    } 

    else if (type == "EFS") { 

        jsont.dataShape.fieldDefinitions['Mesure1'] = { name: 'Mesure1', baseType: 'NUMBER' }; 

        jsont.dataShape.fieldDefinitions['Mesure2'] = { name: 'Mesure2', baseType: 'NUMBER' }; 

    } 

    /**if (row.Batterie) {

            result.dataShape.fieldDefinitions['Batterie'] = { name: 'Batterie', baseType: 'NUMBER' };

    }**/ 

  jsont.dataShape.fieldDefinitions['Timestamp'] = { name: 'Timestamp', baseType: 'DATETIME' }; 

    

    for (var x = 0; x < tableLength; x++) { 

        var row = datas.rows

 

 

        // CustomPropertiesHistory entry object 

        newEntry = new Object(); 

        

        if (type == "Temperature") { 

            newEntry.Temperature = 0; 

            newEntry.Temperature = row.Temperature; // NUMBER 

        } 

        else if (type == "EFS") { 

            //cal1 = row.Mesure1 * 10 * 0.001; 

            cal1 = row.Mesure1 / 10; 

            newEntry.Mesure1 = 0; 

            if (cal1) { 

            newEntry.Mesure1 = cal1; // NUMBER 

            } 

            newEntry.Mesure2 = 0; 

        } 

        

        if (row.Batterie) {            

            newEntry.Batterie = 0; 

            newEntry.Batterie = row.Batterie; // NUMBER 

        } 

        newEntry.Timestamp = row.timestamp; // DATETIME - isPrimaryKey = true 

        //newEntry.Timestamp = dateAddMilliseconds(row.timestamp, -3600000); // DATETIME - isPrimaryKey = true 

        jsont.rows = newEntry; 

    } 

    

    logger.error(jsont.rows[3].Temperature); 

 

 

    //var result = jsont; 

    var params = { 

  json: jsont /* JSON */ 

  }; 

 

 

  // result: INFOTABLE 

  var result = Resources["InfoTableFunctions"].FromJSON(params); 

    logger.error(result[3].Temperature);

View solution in original post

2 REPLIES 2
jamesm1
5-Regular Member
(To:jdurand)

Can you please post the JSON you are trying to convert to an InfoTable?

Edit: Actually, looking at this, I made a few changes to your code:

var jsont = { dataShape: { fieldDefinitions : {} }, rows: [] }; 

jsont.rows = [];

 

    var cal1; 

    

    if (type == "Temperature") { 

    jsont.dataShape.fieldDefinitions['Temperature'] = { name: 'Temperature', baseType: 'NUMBER' }; 

    } 

    else if (type == "EFS") { 

        jsont.dataShape.fieldDefinitions['Mesure1'] = { name: 'Mesure1', baseType: 'NUMBER' }; 

        jsont.dataShape.fieldDefinitions['Mesure2'] = { name: 'Mesure2', baseType: 'NUMBER' }; 

    } 

    /**if (row.Batterie) {

            result.dataShape.fieldDefinitions['Batterie'] = { name: 'Batterie', baseType: 'NUMBER' };

    }**/ 

  jsont.dataShape.fieldDefinitions['Timestamp'] = { name: 'Timestamp', baseType: 'DATETIME' }; 

    

    for (var x = 0; x < tableLength; x++) { 

        var row = datas.rows

 

 

        // CustomPropertiesHistory entry object 

        newEntry = new Object(); 

        

        if (type == "Temperature") { 

            newEntry.Temperature = 0; 

            newEntry.Temperature = row.Temperature; // NUMBER 

        } 

        else if (type == "EFS") { 

            //cal1 = row.Mesure1 * 10 * 0.001; 

            cal1 = row.Mesure1 / 10; 

            newEntry.Mesure1 = 0; 

            if (cal1) { 

            newEntry.Mesure1 = cal1; // NUMBER 

            } 

            newEntry.Mesure2 = 0; 

        } 

        

        if (row.Batterie) {            

            newEntry.Batterie = 0; 

            newEntry.Batterie = row.Batterie; // NUMBER 

        } 

        newEntry.Timestamp = row.timestamp; // DATETIME - isPrimaryKey = true 

        //newEntry.Timestamp = dateAddMilliseconds(row.timestamp, -3600000); // DATETIME - isPrimaryKey = true 

        jsont.rows = newEntry; 

    } 

    

    logger.error(jsont.rows[3].Temperature); 

 

 

    //var result = jsont; 

    var params = { 

  json: jsont /* JSON */ 

  }; 

 

 

  // result: INFOTABLE 

  var result = Resources["InfoTableFunctions"].FromJSON(params); 

    logger.error(result[3].Temperature);

The JSON is generated dynamically on the code. But your changes solved my issue !

Many thanks !

Top Tags