Skip to main content
1-Visitor
July 24, 2014
Question

Error trying to convert a JSON into an Infotable

  • July 24, 2014
  • 1 reply
  • 4717 views

Hello:


I'm using the snippet FROMJson to convert a JSON into an Infotable and I've already created the datashape that actually match the fields on the array of JSON Objects and I still getting this error:

Wrapped org.json.JSONException: JSONObject["dataShape"] not found. Cause: JSONObject["dataShape"] not found.


This is the code behind:


var params = { 

    json: "[{'name': 'Bidhan Chatterjee','email': 'bidhan@example.com'},{'name': 'Rameshwar Ghosh','email': 'datasoftonline@example.com'}]"  /* JSON */

};


// result: INFOTABLE

var result = Resources["InfoTableFunctions"].FromJSON(params);



Thanks.



    1 reply

    5-Regular Member
    July 24, 2014

    Hi Martin,


    FromJSON is intended to be used for JSON opjects generated by ThingWorx that already include a DataShape object. For externally generated JSON, you'll want to use one of the content loader functions that correspond to one of the RESTful API http methods. Please note that the Content loader functions with a LOAD prefix will be deprecated in future releases of ThingWorx.

    Thanks,

    Saeed



    1-Visitor
    July 24, 2014

    Hello:


    So I'm doing this:



    var json = ;

    var params = {

    infoTableName : "InfoTable",

    dataShapeName : "jsontest"

    };

    // CreateInfoTableFromDataShape(infoTableName:STRING("InfoTable"), dataShapeName:STRING):INFOTABLE(jsontest)

    var infotabletest = Resources["InfoTableFunctions"].CreateInfoTableFromDataShape(params);


     for(var i=0;i<json.length;i++){

            var obj = json;

            for(var key in obj){

                var attrName = key;

                var attrValue = obj[key];

           infotabletest.AddRow(yourRowObjectHere);

         

              

            }

        }


    I dunno how to create the yourRowObjectHere.

     



    19-Tanzanite
    July 25, 2014

    Hi Martin,

    If you just want to add an InfoTable row, the new row object is created like this:
    var newEntry = new Object();
    newEntry.DataShapeField1 = "value1"; // NUMBER
    newEntry.DataShapeField2 = "value2";
     and so on.

    I modified the code a bit just for ease of understanding and the following does work:

     for(var i=0;i<json.length;i++){
            var obj = json;
            var newRow = new Object();
            newRow.name=obj.name;
            newRow.email=obj.email;
            infotabletest.AddRow(newRow);
        }
    var result=infotabletest;