Skip to main content
1-Visitor
May 17, 2016
Question

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

  • May 17, 2016
  • 2 replies
  • 5978 views

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.

    2 replies

    22-Sapphire I
    May 17, 2016

    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.

    1-Visitor
    May 18, 2016

    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.

    22-Sapphire I
    May 18, 2016

    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.

    5-Regular Member
    May 18, 2016

    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.