Skip to main content
1-Visitor
August 31, 2018
Solved

JSON to Infotable Problem

  • August 31, 2018
  • 3 replies
  • 2234 views

Hi there, I'm trying to get data from a JSON format into an infotable. Just as a try I want to get the unit "kWh" into the table. For that I designed a data shape with a single text field. Problem is the created table is empty. As raw JSON format the data is presented just fine. Do you see an error? Cheers, Nils

 

Here is my code so far, based on the instructions from here: https://community.ptc.com/t5/ThingWorx-Developers/About-Convert-JSON-file-to-Infotable/m-p/508236?source=Article%2520viewer

 

var params = {
proxyScheme: undefined /* STRING */,
headers: undefined /* JSON */,
ignoreSSLErrors:true, 
useNTLM: undefined /* BOOLEAN */,
workstation: undefined /* STRING */,
useProxy: undefined /* BOOLEAN */,
withCookies: undefined /* BOOLEAN */,
proxyHost: undefined /* STRING */,
url: "http://xxxxx",
timeout: undefined /* NUMBER */,
proxyPort: undefined /* INTEGER */,
password:"xxx",
domain: undefined /* STRING */,
username:"xxx"
};

// result: JSON
var json = Resources["ContentLoaderFunctions"].GetJSON(params);
var params2 = {
infoTableName : "InfoTable",
dataShapeName : "Berg1_Shape"
};

//result INFOTABLE
var jsonTable = Resources["InfoTableFunctions"].CreateInfoTableFromDataShape(params2);

for(var i=0; i<5; i++) {
jsonTable.AddRow({Value:json.values.unit});
}
result=jsonTable;

 

And this is the JSON input:

 

{
"self": {
"href": "https://xxxxx"
},
"version": 2,
"values": [
{
"unit": "kWh",
"time": "2018-08-30T22:15:00Z",
"value": 0.01
},
{
"unit": "kWh",
"time": "2018-08-30T22:30:00Z",
"value": 0.3
},

....

Best answer by Nils_SG

That wasn't the mistake, but thank you anyway.

 

Error was, that the number of entries in the for loop must match the input from the json files, e.g. 3.

3 replies

1-Visitor
September 3, 2018

Well actually you are iterating wrongly over json format, your format is as follows:

 

Screenshot 2018-09-03 10.57.58.png

then ro reach unit on values it's an array, you should be doing something like:

 

json.values[index].unit

 

 

Nils_SG1-VisitorAuthorAnswer
1-Visitor
September 4, 2018

That wasn't the mistake, but thank you anyway.

 

Error was, that the number of entries in the for loop must match the input from the json files, e.g. 3.

1-Visitor
September 4, 2018

Of course, but instead of hard code a 5 or a 3 on the for loop better you use array length property: json.values.length