Skip to main content
1-Visitor
January 30, 2019
Question

Thingworx Template Custom Extension, Issue with JSON String input in custom service

  • January 30, 2019
  • 1 reply
  • 1891 views

I have created a custom thing template which contains a service which takes data in json string, then generates a PDF report ans save it in file repository.

 

The issue is, when I hard code the json string inside the service, the custom extension works great. It creates report and save it to repository, But if the same json string input is passed dynamically from imported extension, it gives me the below error:

 

Unable to Invoke Service CreatePDFReport on PDFThing : java.lang.String cannot be cast to org.json.simple.JSONObject .

 

JSON String ::  "{  \"Date\":\"121212\", \"Shift\":\"One\", \"TeamMembers\":\"Sachin\" }"

 

Please Help.

----------------------------------------------------------------------------------------------------------------------------------

Below is my JAVA code:

 

@ThingworxServiceDefinition(name = "CreatePDFReport", description = "", category = "", isAllowOverride = false, aspects = {
"isAsync:false" })
@ThingworxServiceResult(name = "Result", description = "", baseType = "STRING", aspects = {})
public String CreatePDFReport(
@ThingworxServiceParameter(name = "jsonStringA", description = "", baseType = "STRING", aspects = {
"isRequired:true" }) String jsonStringA) throws IOException, DocumentException, JSONException, ParseException {

_logger.trace("Entering Service: CreatePDFReport");


String DEST = "C:\\ThingworxStorage\\repository\\Test_PDF\\PDFReport.pdf";

String jsonStringTest = "{  \"Date\":\"121212\", \"Shift\":\"One\", \"TeamMembers\":\"Sachin\" }";


JSONParser parser = new JSONParser();

JSONObject myData = (JSONObject) parser.parse(jsonStringA);
//JSONObject myData = (JSONObject) parser.parse(jsonStringTest);


new PDFBoxAtom().createPdfAtom(DEST, myData); // Function which creates PDF report and save it to location DEST.

_logger.trace("Exiting Service: CreatePDFReport");

return "PDF Created";

}

 

Note: If I use jsonStringTest to create PDF report, it works fine But when I pass the same json string dynamically(jsonStringA), it gives me the error.

 

1 reply

1-Visitor
January 30, 2019

What does the java code look like for your CreatePDFReport method?  Also, what does your method signature look like where you declare your JSON input param?  If you post your code, someone will be able to help you.

1-Visitor
January 31, 2019

I have edited the post with my java code.Please have a look

1-Visitor
January 31, 2019

Change your base type from String to JSON for your input param.