9 Replies Latest reply on Jul 25, 2016 2:22 PM by pgupta-2 RSS
    pgupta-2 Apprentice

    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.

      • Re: Converting JSON into Infotable
        Sajid Patel Apprentice

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

        • Re: Converting JSON into Infotable
          meghan Communicator

          Preeti,

           

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

           

          Meghan

            • Re: Converting JSON into Infotable
              pgupta-2 Apprentice

              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?

                • Re: Converting JSON into Infotable
                  meghan Communicator

                  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[i].name,email:json.rows[i].email});

                   

                  }

                   

                  var result = infotabletest;

                   

                   

                  Try using the code above in a script and see if that logs the length and iterates the JSON object.

              • Re: Converting JSON into Infotable
                Mayank Patel Apprentice

                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