cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

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

Highlighted
Level 6

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

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.

 

4 REPLIES 4

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

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.

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

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

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

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

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

Hi @SachinTanwar.

 

If the solution provided by @wposner-2 allowed you to solve your issue, please mark it as the Accepted Solution for the benefit of others on the Community.

 

Regards.

 

--Sharon