cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Help us improve the PTC Community by taking this short Community Survey! X

FromJSON() usage

anderswi
1-Newbie

FromJSON() usage

Hi,

I need to convert a JSON-structure loaded from an external API into an InfoTable that I return from my service. Now I'm creating the InfoTable from a DataShape and manually adding rows in a for each loop, which works well. Then I found this method which seem convenient:

Resources["InfoTableFunctions"].FromJSON(params);


I tried it but got complaints about a missing "dataShape" field in the JSON structure... The error makes sense but I can't figure out the right syntax for communicating which DataShape to use. The method documentation does not give any clues.

Can anybody share some example code that uses this method? Preferably in conjunction with LoadJSON :)

Kind Regards,

Anders

7 REPLIES 7
adam11
5-Regular Member
(To:anderswi)

Hi Anders,

Indeed. FromJSON() seems convenient, but it doesn't do what you expect. That is to say, it doesn't do what you and I would like it to do ;-)


For FromJSON() to execute successfully, the Data Shape of the info table you wish to create must be specified in the JSON object (not just the name, but the entire structure (ie. field definitions)). The use case for this Service is limited – iterating through the JSON object and adding rows to an info table is the way to go.


Thanks,

Adam




But should be rather ease, to build an Infotable with Resources["InfoTableFunctions"].CreateInfoTableFromDataShape(infoTableName,dataShapeName) with the desired DataShape, and at the end this is a JSON object, extract the DataShape definition from this and apply to the JSON object.

I didn't tried but maybe it's the way to go.

Thank you

Thanks for explaining it Adam. A deceptive service indeed! :)

Hello Anders,

How do you call the external REST services ?

I have to make the same the thing but I did not succeed .

I have trid the LoadJSON method .... but it is not working ..

Thanks and regards,

sv
1-Newbie
1-Newbie
(To:anderswi)

Hi,

I am facing with the same issue. I am not able to resolve it. Can some one help me with that

The code that i use :

var params = {

  headers:{'Content-Type':'application/json','Accept':'application/json','appkey':'******'},

  url:'http://***************/Thingworx/Things/Logistics/Properties/test',

};

// result: JSON

var Json = Resources["ContentLoaderFunctions"].GetJSON(params);

var Infotable=Resources["InfoTableFunctions"].FromJSON({json:Json});

var testString=Infotable.test;

var result=testString.split("|");

Things["Logistics"].Device_ID=result[0];

Things["Logistics"].Flag=result[1];

Things["Logistics"].Theft_Flag=result[2];

Things["Logistics"].Temperature=result[3];

Things["Logistics"].Humidity=result[4];

Things["Logistics"].Battery_volt=result[5];

Things["Logistics"].Data=result[6];

This code was working fine with Thingworx composer 6. But now it showing this error "Wrapped org.json.JSONException: JSONObject["dataShape"] not found. Cause: JSONObject["dataShape"] not found."

ibanham
1-Newbie
(To:sv)

Hi

As per Adam's reply earlier in the thread, FromJSON doesn't do what you think it does. It only works if the JSON is already in the format of a ThingWorx InfoTable with an array called (I think)  "fields" that defines all of the properties.

Simpler not to use this method and just parse the JSON that you're getting back.

Regards

Ian

Top Tags