Skip to main content
15-Moonstone
July 13, 2016
Question

Converting JSON into Infotable

  • July 13, 2016
  • 10 replies
  • 7950 views

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

12-Amethyst
July 13, 2016

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

5-Regular Member
July 14, 2016

Preeti,

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

Meghan

15-Moonstone
July 14, 2016

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?

5-Regular Member
July 14, 2016

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.

1-Visitor
July 14, 2016

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