Why do I have to assign incoming request.body.dataString to a variable before using JSON.parse()?

Andy Hopper
Tera Contributor

I'm creating an inbound scripted REST resource using application/json format.

 

I've noticed that I cannot simply do the following to create an object from the data:

 

var requestDataObj = JSON.parse(request.body.dataString);

return {
    message: requestDataObj.number
};

 

 

If I do so, I get an HTTP 500 server error on the client-side, the following response, and error in the syslog table:

 

  "error": {
    "message": "Empty JSON string",
    "detail": "SyntaxError: Empty JSON string (sys_ws_operation.7b6a102b93420210416272918bba10a8.operation_script; line 7)"
  },
  "status": "failure"
}

 

Empty JSON string: com.glide.rest.domain.ServiceException: Empty JSON string: com.glide.rest.service.custom.CustomServiceExceptionResolver.throwServiceException(CustomServiceExceptionResolver.java:82)
com.glide.rest.service.custom.CustomServiceExceptionResolver.throwServiceException(CustomServiceExceptionResolver.java:77)
com.glide.rest.service.custom.CustomServiceExceptionResolver.resolveForException(CustomServiceExceptionResolver.java:45)
com.glide.rest.service.custom.CustomServiceResultHandler.handle(CustomServiceResultHandler.java:22)
com.glide.rest.service.custom.CustomService.execute(CustomService.java:78)
com.glide.rest.handler.impl.ServiceHandlerImpl.invokeService(ServiceHandlerImpl.java:37)
com.glide.rest.processors.RESTAPIProcessor.process(RESTAPIProcessor.java:345)
com.glide.processors.AProcessor.runProcessor(AProcessor.java:762)
com.glide.processors.AProcessor.processTransaction(AProcessor.java:313)
com.glide.processors.ProcessorRegistry.process0(ProcessorRegistry.java:187)
com.glide.processors.ProcessorRegistry.process(ProcessorRegistry.java:175)
com.glide.ui.GlideServletTransaction.process(GlideServletTransaction.java:58)
com.glide.sys.Transaction.run(Transaction.java:2734)
com.glide.ui.HTTPTransaction.run(HTTPTransaction.java:35)
java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
java.base/java.lang.Thread.run(Thread.java:840)

 

If I assign the contents of the request.body.dataString property to a variable first, everything works fine!

 

var requestDataString = request.body.dataString;
var requestDataObj = JSON.parse(requestDataString);

return {
    message: requestDataObj.number
};

 

HTTP 200, Response:

{
  "result": {
    "message": "INC0146564"
  }
}

 

Can anyone explain to me why this makes a difference?

 

I am creating all of this in a custom scope, if that makes any difference.

(All POST submissions were made using the same content, submitted via the REST API Explorer within the same instance.)

1 ACCEPTED SOLUTION

Maik Skoddow
Tera Patron
Tera Patron

Please try

var requestDataObj = JSON.parse(String(request.body.dataString));

View solution in original post

5 REPLIES 5

Maik Skoddow
Tera Patron
Tera Patron

Please try

var requestDataObj = JSON.parse(String(request.body.dataString));

Yep, that works.

Can you explain why this is necessary? If request.body.dataString is already presented as a string, then why do we need to coerce the type?

Thanks!

To be honest, I cannot. JavaScript is a terrible language regarding data type safety, and at the end you NEVER can rely on any datatype returned by an API. That's the reason why I'm using as often as possible the explicit cast to a String type via String();

Well, enough time has passed that I guess nobody else knows either! 🙂
Thanks for your prompt response, Maik. I'll accept your answer as, ultimately, it did the job.