Skip to main content
1-Visitor
April 6, 2021
Solved

JSON to InfoTable

  • April 6, 2021
  • 1 reply
  • 7950 views

Hello, I followed various articles from community forum here as well as this TWX article. and I am still having issues with my code outputting to an InfoTable. I am using an API to fetch Stock Price JSON data and having it be converted to an InfoTable, so it can be displayed into a graph in Mashups. 

 

I've attached screenshots of my DataShape Field Definitions as well as the JSON Data Output.

 

The TWX code, with it outputting the columns with "No Data" is below. I've tried playing around with the for loop and doing a data.array.length but that mentions length is undefined. I am not sure if that would output to it being an InfoTable either. Thank you.

 

var restParams = {

 proxyScheme: undefined /* STRING */,
 headers: undefined /* JSON */,
 ignoreSSLErrors: true /* BOOLEAN */,
 useNTLM: undefined /* BOOLEAN */,
 workstation: undefined /* STRING */,
 useProxy: undefined /* BOOLEAN */,
 withCookies: undefined /* BOOLEAN */,
 proxyHost: undefined /* STRING */,
 url: "https://finnhub.io/api/v1/quote?symbol=AAPL&token=c1mbi3i37fkpnsp59e6g" /* STRING */,
 content: undefined /* JSON */,
 timeout: undefined /* NUMBER */,
 proxyPort: undefined /* INTEGER */,
 password: undefined /* STRING */,
 domain: undefined /* STRING */,
 username: undefined /* STRING */

};

// result: JSON

var data = Resources["ContentLoaderFunctions"].GetJSON(restParams);

var params = {

infoTableName: "InfoTable" /* STRING */,
dataShapeName: "GrahicVisualizer_Stock_DS" /* DATASHAPENAME */

};

// result: INFOTABLE

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

//var tableLength = CobotInfoTable.rows.length;
//fix code below
for(var i=0;i<data.length;i++)

{

jsonTable.AddRow({
 c:data.c, 
 h:data.h,
 l:data.l, 
 o:data.o,
 pc:data.pc,
 t:data.t
});

}

result = jsonTable;

 

Screen Shot 2021-04-06 at 7.43.35 PM.png

 

Screen Shot 2021-04-06 at 7.42.10 PM.png

 

Best answer by MFritzhand

@tdixit ,

 

I managed to solve the issue on my end. InfoTable really only function with arrays, that is why all the solutions on forums I were seeing here were mentioning the for loop to parse it. The API I was pulling from used just one specific object, so I was unable to turn that into an array. 

 

Through trial and error, I tried Stringify method, which didn't work really. I then proceeded utilizing Parse method. This achieved getting each number within the object by itself. I set a variable for each number. I was able to call each specific number... from there I tried converting to infotable using the AddRow snippet. Encountered an error, "unable to convert DOUBLE to JSON". I thought okay... I switched the datatypes on the datashape field defintions to number, so Double can be converted. I ran it, was successful. 

 

me.myjson = Resources["ContentLoaderFunctions"].GetJSON(params);

var myJSON = JSON.parse(me.myjson);
//result = data;
//result = myJSON;
var currentPrice = me.myjson.c;
var highPrice = me.myjson.h;
var lowPrice = me.myjson.l;
var openPrice = me.myjson.o;
var previousClose = me.myjson.pc;
var timeStamp = me.myjson.t;

var params = {
 infoTableName : "InfoTable",
 dataShapeName : "GrahicVisualizer_Stock_DS"
};
// CreateInfoTableFromDataShape(infoTableName:STRING("InfoTable"), dataShapeName:STRING):INFOTABLE(GrahicVisualizer_Stock_DS)
var infoTable = Resources["InfoTableFunctions"].CreateInfoTableFromDataShape(params);

var newRow = new Object();
newRow.c = currentPrice;
newRow.h = highPrice;
newRow.l = lowPrice;
newRow.o = openPrice;
newRow.pc = previousClose;
newRow.t = timeStamp;

infoTable.AddRow(newRow);

result = infoTable;

 

1 reply

5-Regular Member
April 7, 2021

Hello @MFritzhand 

 

Thank you for reaching out to PTC

 

I will try this at my end and will get back to you.

 

Meanwhile could you please help me with your ThingWorx version

 

Regards,

Toolika Dixit

1-Visitor
April 7, 2021

I am on TWX 9.0 @tdixit 

MFritzhand1-VisitorAuthorAnswer
1-Visitor
April 12, 2021

@tdixit ,

 

I managed to solve the issue on my end. InfoTable really only function with arrays, that is why all the solutions on forums I were seeing here were mentioning the for loop to parse it. The API I was pulling from used just one specific object, so I was unable to turn that into an array. 

 

Through trial and error, I tried Stringify method, which didn't work really. I then proceeded utilizing Parse method. This achieved getting each number within the object by itself. I set a variable for each number. I was able to call each specific number... from there I tried converting to infotable using the AddRow snippet. Encountered an error, "unable to convert DOUBLE to JSON". I thought okay... I switched the datatypes on the datashape field defintions to number, so Double can be converted. I ran it, was successful. 

 

me.myjson = Resources["ContentLoaderFunctions"].GetJSON(params);

var myJSON = JSON.parse(me.myjson);
//result = data;
//result = myJSON;
var currentPrice = me.myjson.c;
var highPrice = me.myjson.h;
var lowPrice = me.myjson.l;
var openPrice = me.myjson.o;
var previousClose = me.myjson.pc;
var timeStamp = me.myjson.t;

var params = {
 infoTableName : "InfoTable",
 dataShapeName : "GrahicVisualizer_Stock_DS"
};
// CreateInfoTableFromDataShape(infoTableName:STRING("InfoTable"), dataShapeName:STRING):INFOTABLE(GrahicVisualizer_Stock_DS)
var infoTable = Resources["InfoTableFunctions"].CreateInfoTableFromDataShape(params);

var newRow = new Object();
newRow.c = currentPrice;
newRow.h = highPrice;
newRow.l = lowPrice;
newRow.o = openPrice;
newRow.pc = previousClose;
newRow.t = timeStamp;

infoTable.AddRow(newRow);

result = infoTable;