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

How to access the JSON data passing from external Java Code Via REST Call To TWX Service

pagrawal-2
2-Guest

How to access the JSON data passing from external Java Code Via REST Call To TWX Service

I have written this code Snippet in Java:

public static void callTW(JSONObject obj) throws Exception {

  String statusMsg = "";

  try

  {  

  String uri = "http://localhost:8080/Thingworx/Things/test2/Services/JSON_Split?userid=Administrator&password=admin";

  HttpURLConnection conn = (HttpURLConnection) new URL(uri).openConnection();

  conn.setDoOutput(true);

  conn.setRequestMethod("POST");

  conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");

  OutputStream os = conn.getOutputStream();

  JSONObject obj2=new JSONObject();

  obj2.put("IDA2A2", "6278");

  obj2.put("Number_P", "W10320887");

  obj2.put("Name", "test");

  JSONObject obj3=new JSONObject();

  obj3.put("IDA2A2", "6278");

  obj3.put("Number_P", "W10320887");

  obj3.put("View_P", "Engineering");

  obj.put("partmaster", obj2);

  obj.put("part", obj3);

  System.out.println(obj);

  os.flush();

  if (conn.getResponseCode() != HttpURLConnection.HTTP_OK)

  {

  statusMsg = conn.getResponseCode() + " " + conn.getResponseMessage();

  throw new RuntimeException("Failed : HTTP error code : " + statusMsg);

  }

  os.close();

  conn.disconnect();

  }

  catch (Exception e)

  {

  throw new Exception(statusMsg + "." + e);

  }

}

This Code actually contains three JSON Object and here I am actually merging two JSON Object into one Object i.e. "obj".

And Now I want to catch this whole JSON at ThingWorx Side by creating a Service.

I actually want to split this JSON Object "obj" into its Child object and then store the values in these child objects at TWX Side.

I hope you guys are getting me.

What script I should write at TWX Side to catch all this data.

9 REPLIES 9
PaiChung
22-Sapphire I
(To:pagrawal-2)

Create a Service with an Input parameter of basetype JSON in Thingworx

Thingworx should recognize the payload as JSON and allow you to traverse it as an object.

I usually start out setting that as String and log it out, so I can make sure I'm receiving what I'm expecting and take the next step from there.

Hey Pai,

Thanks for replying to my question.

But Actually I created a service and in that I took an input parameter say "v1" of base type "JSON".

Then I just wrote :: logger.warn(v1); as a script, but I got undefined in the logs.

If you see the code snippet which is ::

  JSONObject obj2=new JSONObject();          // First JSON Object Containing three attributes

  obj2.put("IDA2A2", "6278");

  obj2.put("Number_P", "W10320887");

  obj2.put("Name", "test");

  JSONObject obj3=new JSONObject();          // Second JSON Object Containing three attributes

  obj3.put("IDA2A2", "6278");

  obj3.put("Number_P", "W10320887");

  obj3.put("View_P", "Engineering");

  obj.put("partmaster", obj2);                         // Third JSON Object Containing that two JSON Objects ( Like merging that two object and put in one object)

  obj.put("part", obj3);                                   // obj contains "obj2" and "obj3".

I took a input parameter of JSON type at TWX Side in a service then to which JSON obj in the Java code the I/P JSON Variable is referring if I would do that.

I hope you are getting me I want to print this all data which is actually holding by "obj" in java code at ThingWorx side,but unfortunately I am not getting the way.

Can you please more specific about the solution or script,it would be really helpful for me.

Thanks.

PaiChung
22-Sapphire I
(To:pagrawal-2)

When the input parameter of the service is set to STRING vs. JSON does it log out anything at all?

That is how I usually start on the Thingworx side.

Hey Pai,

String vs. JSON ????

I took i/p parameter named "v1" and a string parameter named "IDA2A2" and I did logger.warn(v1.IDA2A2) and it is giving me an error called can't read property IDA2A2.

PaiChung
22-Sapphire I
(To:pagrawal-2)

Declare v1 as basetype string and do logger.warn(v1)

Yeah I did that but it is showing "undefined" in the logs.

This is because we are putting wrong i/p or script at TWX side which is not supporting the structure of JSON we defined in the Code.

See this is I am retrieving.

We have to crack that code ::

JSONObj1:Contains some Attribute

JSONObj2:Contains some Attribute

JSONObj3 Contains JSONObj1 & JSONObj2

Now we have to write something which can communicate with this JSONObj3 then retrieve other JSONObj 1& 2.

Thanks.

PaiChung
22-Sapphire I
(To:pagrawal-2)

So your POST is executing, but the obj defined is not being properly passed in as a parameter correct?

I would do a search on REST API in this forum, there is quite a few entries on that.

Ok but I need really help from your side,because you are the only one who can only solve this issue.

I am waiting for your reply with some good solution.

Thanks Pai.

mhollenbach
5-Regular Member
(To:pagrawal-2)

I may be reading your post wrong, but it doesn't look like you are actually sending the JSON object in the body of the REST request at all. You should try using the .write method of the OutputStream class.

Top Tags