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 an answer that solved your problem? Please mark it as an Accepted Solution so others with the same problem can find the answer easily. X

JSON to Infotable Problem

Nils_SG
7-Bedrock

JSON to Infotable Problem

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
},

....

ACCEPTED SOLUTION

Accepted Solutions

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.

View solution in original post

3 REPLIES 3

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

 

 

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.

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

Announcements


Top Tags