2 Replies Latest reply on Feb 20, 2017 7:46 AM by jdurand RSS
    jdurand Explorer

    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[x];
    
    
            // 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[x] = 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.

      • Re: FromJSON() returns empty infotable
        jamesm Creator

        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[x];  
          
          
                // 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[x] = 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);