Community Tip - You can Bookmark boards, posts or articles that you'd like to access again easily! X
I followed the article published here https://support.ptc.com/appserver/cs/view/solution.jsp?n=CS238970
I am using Thingworx 7.1
For some reason "result.Length" is coming as undefined? I tried uppercase and lower case both. So it does not go into for loop to add content in the infortable.
var result = Resources["ContentLoaderFunctions"].GetJSON(params);
logger.debug(' object ' +Object.keys(result).length);
logger.debug('Result' +result.Length);
thanks.
I suggest that you check the actual content of JSON, you can use JSON.stringify() to log it.
Object.keys(<json>).length should work.
for e.g.
logger.info(" result size = " + Object.keys(result).length);
logger.info(" result headers size = " + Object.keys(result.headers).length);
Or if the field is an array, you can simply use 'length' as follows
logger.info(" result headers size = " + result.headers.length);
Preeti,
Try using result.rows.length. That should give you the result you need.
Meghan
logger.debug(' object ' +Object.keys(result).length);
logger.debug('Result' +result.rows.length);
typeError: Cannot read property "length" from undefined
I have opened tech support call too. Is there any other way to parse JSON?
From that example article you attached, the following works for me in version 7.1.3 of ThingWorx:
var json = {
"rows":[
{
"email":"example1@ptc.com"
},
{
"name":"blah",
"email":"example2@ptc.com"
}
]
}
var params = {
infoTableName: "InfoTable",
dataShapeName : "jsontest"
};
var infotabletest = Resources["InfoTableFunctions"].CreateInfoTableFromDataShape(params);
logger.warn(json.rows.length);
for(var i=0; i<json.rows.length; i++) {
infotabletest.AddRow({name:json.rows.name,email:json.rows.email});
}
var result = infotabletest;
Try using the code above in a script and see if that logs the length and iterates the JSON object.
Meghan, This one works now.
I am fairly new into this. I have JSON which I can see as String output below in ThingWorx.
{"headers":"","array":[{"Description":"D","Id":1},{"Description":"C","Id":2},{"Description":"S","Id":3},{"Description":"I","Id":4}]}
When I use the code below.
// result: JSON
var result = Resources["ContentLoaderFunctions"].GetJSON(params);
logger.debug(' object ' +Object.keys(result).length);
logger.debug('Result' +result.array.Length);
Second logger Result comes as undefined. while the example in the CS article returns correctly second logger value.
You have a capital L in length. Try changing it to lowercase.
Meghan
In the service below, result is also an array and you were getting undefined because an index was not selected
logger.debug('Result' +result[0].array.length);
I got it working. Thanks all for your help.
Hello,
Could you help and tell me how did you manage it, please? as I am struggling the same issue you had!
Thanks in advance
Karim
Steps for execute Load JSON in Thingworx or any Platform,
1) First try to Execute API in Browser like in Firefox have Poster and in Google Crome have Postman Addon , who use for execute our service ( Addon Widely use by developer for testing RestAPI ).
- In this exercise you understand which parameter require what will be result provide by API.
2) When you successfully execute your API into Browser then use those parameter use in Poster (Firefox Plugin) and Apply in Thingworx Javascript code.
Thanks,
Mayank