Respond with JSON without using an InfoTable Template
I have a number of services in my solution that are invoked via RESTful POST requests from an external application. The services must respond with a set of values. Each service responds with a different set of values (and as the solution matures over time, these responses could become more diverse and more numerous).
The response from the services must be in a JSON format. I've set up the services to have the result type = JSON, but everything I try generates an error with the service. Here is a simplified example of what may be included in the response:
{
"siteId": <string>, // calculated by the service
"areaName": <string>, // pulled from a different Thing
"percentFailed": <number> // calculated by the service
}
If I create this structure as a JavaScript object, I was expecting to use result = JSON.stringify(responseObj) to create a JSON object that could be used for the response. The service crashes when converting the object to JSON just prior to finishing (i.e. the ThingWorx behind-the-scenes code can't convert it to a JSON response type).
If I use the same as above, but set the result type to STRING in the service composer window, it doesn't crash, but the response contains lots of extraneous information. For example:
{\"dataShape\":{
\"fieldDefinitions\":{
\"result\":{
\"name\":\"result\",
\"description\":\"\",
\"baseType\":\"STRING\",
\"ordinal\":0,
\"aspects\":{}}}},
\"rows\":[{
\"result\":\"{
\\\"siteId\\\":\\\"Site-47\\\",
\\\"areaName\\\":\\\"Area 51\\\",
\\\"percentFailed\\\":\\\"23.75\\\"
}\"
}]
}
}
}
}
This is generating additional unnecessary parsing in the requesting application. I would prefer to only have the result object returned.
I'm of the understanding that I can create an InfoTable Template for the response, temporarily instantiate a table, populate it, and the return the table. This will generate a JSON response. However, I'd like to avoid this approach if possible because now I'm defining the response in two places (the template and the JavaScript). Every time a developer needs to change the response, both places need to be updated. Additionally, the solution would end up with numerous InfoTable Templates (one per service) that will need to be maintained. This feels like a poor design choice to me and will increase future development unnecessarily.
What I'd like is to find a solution that doesn't require an InfoTable (uses only JavaScript objects) and returns a JSON object that only contains the response values.
Any ideas are greatly appreciated!

