Skip to main content
5-Regular Member
October 3, 2022
Question

How to create Thingworx extension for converting data from json object to infotable

  • October 3, 2022
  • 3 replies
  • 1330 views

Hi Everyone,

 

I am having troubles while creating Java extension for converting data from JSON to INFOTABLE, I have tried multiple methods and no seem to work.

 

@ThingworxServiceDefinition(name = "JsontoInfo", description = "", category = "", isAllowOverride = false, aspects = {
"isAsync:false" })
@ThingworxServiceResult(name = "Result", description = "", baseType = "INFOTABLE", aspects = {
"isEntityDataShape:true", "dataShape:DemoDS" })
public InfoTable JsontoInfo(
@ThingworxServiceParameter(name = "input", description = "", baseType = "JSON", aspects = {
"defaultValue:{\"name\":\"Abcd\",\"salary\":25000,\"married\":true}" }) InfoTable input) {
_logger.trace("Entering Service: JsontoInfo");
_logger.trace("Exiting Service: JsontoInfo");
return input;
}

ERROR:  Unable to Invoke Service JsontoInfo on TestResJson2info : Cannot cast org.json.JSONObject to                        com.thingworx.types.InfoTable

 

if anybody Know any solution  regarding this, Kindly share it  ASAP.

 

Thanks & Regards 

Raviteja L.

3 replies

17-Peridot
October 3, 2022

We can convert JSON into INFOTABLE in javascript service itself.

Here some articles.

Why do you want to do this with Extension?

16-Pearl
October 10, 2022
@ThingworxServiceDefinition(name = "JsontoInfo", description = "", category = "", isAllowOverride = false, aspects = {
"isAsync:false" })
@ThingworxServiceResult(name = "Result", description = "", baseType = "INFOTABLE", aspects = {
"isEntityDataShape:true", "dataShape:DemoDS" })
public InfoTable JsontoInfo(
@ThingworxServiceParameter(name = "input", description = "", baseType = "JSON", aspects = {
"defaultValue:{\"name\":\"Abcd\",\"salary\":25000,\"married\":true}" }) JSONPrimitive jsonInput) {
 _logger.trace("Entering Service: JsontoInfo");
 // TODO convert jsonInput to infotable
 _logger.trace("Exiting Service: JsontoInfo");
 return new InfoTable();
}

With the code you provided the parameter input should be of type JsonPrimitive. In your JAva code you then have to convert your json into your wanted infotable (just marked as TODO).

17-Peridot
October 13, 2022

There is a function for the InfoTable data type called ".fromJSON()", this is however a deprecated function:

 

http://support.ptc.com/help/thingworx_hc/javadoc/com/thingworx/types/InfoTable.html

 

So I am not sure if you can still use it to build your infotable data type from a JSON data type.

19-Tanzanite
October 19, 2022

Hi @RL_10230695 ,

Using Java extensions to achieve something that can be done in a ThingWorx service is generally not recommended (outside of situations where you just can't achieve what you want with ThingWorx's internal libraries).

I'm saying this because I have witnessed quite a few applications with their logic built entirely in Java due to unclear reasons.

Since you have the same capabilities in Javascript, it makes no sense to build that logic in Java extension, as it's harder to maintain, deploy and modify later down the road.

If you're looking to convert a JSON to infotable, just iterate over the JSON rows and for each JSON row add a row in your infotable.