Skip to main content
1-Visitor
September 23, 2016
Solved

LOAD JSON/CSV FILE IN THINGWORX

  • September 23, 2016
  • 2 replies
  • 5084 views

Hello everyone!!

I'm trying to create a demo having data in csv(or json) format.

I would like to import data's file in the composer, parse it and see data in a mashup with a grid.

Also i would like to create a stream that show me (in the meshup) the parameter's trend.

I created a thing with a repository template (because the file is in C:/ThingworxStorage/repository) but when i try the service loadJSON anything appeare to me.

Can anyone help me??

Thank's!!

Best answer by keriw

you need to hit the save button for the service after creating it before you can hit the execute, that's the classic error you see, if you haven't done that.

2 replies

1-Visitor
September 23, 2016

Maurizio,

Here is an example of pulling data from a csv file stored in a repository and then outputting it as an infotable in a service.

var params = {

  path: "RPTM.csv" /* STRING */,

  columnMappings: "Location;StartTime;EndTime;ProductCode;CasesProduced;PerHourCWS;CWS;Efficiency"/* STRING */,

  hasHeader: true /* BOOLEAN */,

  longitudeField: undefined /* NUMBER */,

  dateFormat: undefined /* STRING */,

  fileRepository: "RPTMFileRepository"/* THINGNAME */,

  latitudeField: undefined /* NUMBER */,

  fieldDelimiter: "," /* STRING */,

  stringDelimiter: "\"" /* STRING */,

  dataShape: "RPTMImport" /* DATASHAPENAME */

};

// result: INFOTABLE

var rptmData = Resources["CSVParserFunctions"].ReadCSVFile(params);

var params = {

  infoTableName : "InfoTable",

  dataShapeName : "RPTM"

};

// CreateInfoTableFromDataShape(infoTableName:STRING("InfoTable"), dataShapeName:STRING):INFOTABLE(RPTM)

var modifiedData = Resources["InfoTableFunctions"].CreateInfoTableFromDataShape(params);

//translating the date fields in csv to date fields in ThingWorx

for each (var row in rptmData.rows) {

   var startTime = new Date(row.StartTime);

   var endTime = new Date(row.EndTime);

   row.StartTime = startTime;

   row.EndTime = endTime;

   

    modifiedData.AddRow({

        Location: row.Location,

        StartTime: startTime,

        EndTime: endTime,

        ProductCode: row.ProductCode,

        CasesProduced: row.CasesProduced,

        PerHourCWS: row.PerHourCWS,

        CWS: row.CWS,

        Efficiency: row.Efficiency,

        Baseline: 100.00

     

    });

}

result = modifiedData;

Thanks,

Keri

mvolanti1-VisitorAuthor
1-Visitor
September 23, 2016

Thank's Kery!

I create a service with your script modificate with my name's file and called it GetDataCSV but when i test the servite  appear:

Error executing service

Invalid service name: GetDataCSV

I try to change name service but the error it's the same.

Have you got any idea??

Thank's very much!!

keriw1-VisitorAnswer
1-Visitor
September 23, 2016

you need to hit the save button for the service after creating it before you can hit the execute, that's the classic error you see, if you haven't done that.

1-Visitor
September 23, 2016