Skip to main content
Jul 13, 2016 9:08 PM UTC
Question

Converting JSON into Infotable

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.

10 replies

Jul 13, 2016 10:31 PM UTC

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);

Jul 14, 2016 2:18 AM UTC

Preeti,

Try using result.rows.length. That should give you the result you need.

Meghan

Jul 14, 2016 3:19 AM UTC

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?

Jul 14, 2016 3:45 AM UTC

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.

Jul 14, 2016 5:51 AM UTC

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