7 Replies Latest reply on Jul 4, 2016 4:21 AM by ianban RSS
    anderswi 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



      • FromJSON() usage
        adam Creator

        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




        • FromJSON() usage
          anderswi Newbie

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

          • Re: FromJSON() usage
            pierregar Explorer

            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,

            • Re: FromJSON() usage
              sv Apprentice

              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."

                • Re: FromJSON() usage
                  ianban Creator

                  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