<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>question Re: Why do I have to assign incoming request.body.dataString to a variable before using JSON.parse() in Developer forum</title>
    <link>https://www.servicenow.com/community/developer-forum/why-do-i-have-to-assign-incoming-request-body-datastring-to-a/m-p/2931049#M1113596</link>
    <description>&lt;P&gt;Yep, that works.&lt;/P&gt;&lt;P&gt;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?&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
    <pubDate>Wed, 15 May 2024 12:40:20 GMT</pubDate>
    <dc:creator>Andy Hopper</dc:creator>
    <dc:date>2024-05-15T12:40:20Z</dc:date>
    <item>
      <title>Why do I have to assign incoming request.body.dataString to a variable before using JSON.parse()?</title>
      <link>https://www.servicenow.com/community/developer-forum/why-do-i-have-to-assign-incoming-request-body-datastring-to-a/m-p/2930743#M1113479</link>
      <description>&lt;P&gt;I'm creating an inbound scripted REST resource using application/json format.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've noticed that I cannot simply do the following to create an object from the data:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;var requestDataObj = JSON.parse(request.body.dataString);

return {
    message: requestDataObj.number
};&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If I do so, I get an HTTP 500 server error on the client-side, the following response, and error in the syslog table:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;SPAN class=""&gt;  "error": {
    "message": "Empty JSON string",
    "detail": "SyntaxError: Empty JSON string (sys_ws_operation.7b6a102b93420210416272918bba10a8.operation_script; line 7)"
  },
  "status": "failure"
}&lt;/SPAN&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE border="1" width="100%"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD width="100%" height="512px"&gt;&lt;FONT face="courier new,courier" size="2"&gt;&lt;SPAN&gt;Empty JSON string: com.glide.rest.domain.ServiceException: Empty JSON string: com.glide.rest.service.custom.CustomServiceExceptionResolver.throwServiceException(CustomServiceExceptionResolver.java:82)&lt;/SPAN&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;&lt;SPAN&gt;com.glide.rest.service.custom.CustomServiceExceptionResolver.throwServiceException(CustomServiceExceptionResolver.java:77)&lt;/SPAN&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;&lt;SPAN&gt;com.glide.rest.service.custom.CustomServiceExceptionResolver.resolveForException(CustomServiceExceptionResolver.java:45)&lt;/SPAN&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;&lt;SPAN&gt;com.glide.rest.service.custom.CustomServiceResultHandler.handle(CustomServiceResultHandler.java:22)&lt;/SPAN&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;&lt;SPAN&gt;com.glide.rest.service.custom.CustomService.execute(CustomService.java:78)&lt;/SPAN&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;&lt;SPAN&gt;com.glide.rest.handler.impl.ServiceHandlerImpl.invokeService(ServiceHandlerImpl.java:37)&lt;/SPAN&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;&lt;SPAN&gt;com.glide.rest.processors.RESTAPIProcessor.process(RESTAPIProcessor.java:345)&lt;/SPAN&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;&lt;SPAN&gt;com.glide.processors.AProcessor.runProcessor(AProcessor.java:762)&lt;/SPAN&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;&lt;SPAN&gt;com.glide.processors.AProcessor.processTransaction(AProcessor.java:313)&lt;/SPAN&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;&lt;SPAN&gt;com.glide.processors.ProcessorRegistry.process0(ProcessorRegistry.java:187)&lt;/SPAN&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;&lt;SPAN&gt;com.glide.processors.ProcessorRegistry.process(ProcessorRegistry.java:175)&lt;/SPAN&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;&lt;SPAN&gt;com.glide.ui.GlideServletTransaction.process(GlideServletTransaction.java:58)&lt;/SPAN&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;&lt;SPAN&gt;com.glide.sys.Transaction.run(Transaction.java:2734)&lt;/SPAN&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;&lt;SPAN&gt;com.glide.ui.HTTPTransaction.run(HTTPTransaction.java:35)&lt;/SPAN&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;&lt;SPAN&gt;java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)&lt;/SPAN&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;&lt;SPAN&gt;java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)&lt;/SPAN&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;&lt;SPAN&gt;java.base/java.lang.Thread.run(Thread.java:840)&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If I assign the contents of the request.body.dataString property to a variable first, everything works fine!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;var requestDataString = request.body.dataString;
var requestDataObj = JSON.parse(requestDataString);

return {
    message: requestDataObj.number
};&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;HTTP 200, Response:&lt;/P&gt;&lt;PRE&gt;&lt;SPAN class=""&gt;{
  "result": {
    "message": "INC0146564"
  }
}&lt;/SPAN&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Can anyone explain to me why this makes a difference?&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am creating all of this in a custom scope, if that makes any difference.&lt;/P&gt;&lt;P&gt;(All POST submissions were made using the same content, submitted via the REST API Explorer within the same instance.)&lt;/P&gt;</description>
      <pubDate>Wed, 15 May 2024 09:39:40 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/why-do-i-have-to-assign-incoming-request-body-datastring-to-a/m-p/2930743#M1113479</guid>
      <dc:creator>Andy Hopper</dc:creator>
      <dc:date>2024-05-15T09:39:40Z</dc:date>
    </item>
    <item>
      <title>Re: Why do I have to assign incoming request.body.dataString to a variable before using JSON.parse()</title>
      <link>https://www.servicenow.com/community/developer-forum/why-do-i-have-to-assign-incoming-request-body-datastring-to-a/m-p/2930771#M1113490</link>
      <description>&lt;P&gt;Please try&lt;/P&gt;
&lt;LI-CODE lang="javascript"&gt;var requestDataObj = JSON.parse(String(request.body.dataString));&lt;/LI-CODE&gt;</description>
      <pubDate>Wed, 15 May 2024 09:59:48 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/why-do-i-have-to-assign-incoming-request-body-datastring-to-a/m-p/2930771#M1113490</guid>
      <dc:creator>Maik Skoddow</dc:creator>
      <dc:date>2024-05-15T09:59:48Z</dc:date>
    </item>
    <item>
      <title>Re: Why do I have to assign incoming request.body.dataString to a variable before using JSON.parse()</title>
      <link>https://www.servicenow.com/community/developer-forum/why-do-i-have-to-assign-incoming-request-body-datastring-to-a/m-p/2931049#M1113596</link>
      <description>&lt;P&gt;Yep, that works.&lt;/P&gt;&lt;P&gt;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?&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Wed, 15 May 2024 12:40:20 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/why-do-i-have-to-assign-incoming-request-body-datastring-to-a/m-p/2931049#M1113596</guid>
      <dc:creator>Andy Hopper</dc:creator>
      <dc:date>2024-05-15T12:40:20Z</dc:date>
    </item>
    <item>
      <title>Re: Why do I have to assign incoming request.body.dataString to a variable before using JSON.parse()</title>
      <link>https://www.servicenow.com/community/developer-forum/why-do-i-have-to-assign-incoming-request-body-datastring-to-a/m-p/2931062#M1113599</link>
      <description>&lt;P&gt;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();&lt;/P&gt;</description>
      <pubDate>Wed, 15 May 2024 12:43:47 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/why-do-i-have-to-assign-incoming-request-body-datastring-to-a/m-p/2931062#M1113599</guid>
      <dc:creator>Maik Skoddow</dc:creator>
      <dc:date>2024-05-15T12:43:47Z</dc:date>
    </item>
    <item>
      <title>Re: Why do I have to assign incoming request.body.dataString to a variable before using JSON.parse()</title>
      <link>https://www.servicenow.com/community/developer-forum/why-do-i-have-to-assign-incoming-request-body-datastring-to-a/m-p/2995510#M1131880</link>
      <description>&lt;P&gt;Well, enough time has passed that I guess nobody else knows either! &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;BR /&gt;Thanks for your prompt response, Maik. I'll accept your answer as, ultimately, it did the job.&lt;/P&gt;</description>
      <pubDate>Mon, 22 Jul 2024 08:23:31 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/why-do-i-have-to-assign-incoming-request-body-datastring-to-a/m-p/2995510#M1131880</guid>
      <dc:creator>Andy Hopper</dc:creator>
      <dc:date>2024-07-22T08:23:31Z</dc:date>
    </item>
    <item>
      <title>Re: Why do I have to assign incoming request.body.dataString to a variable before using JSON.parse()</title>
      <link>https://www.servicenow.com/community/developer-forum/why-do-i-have-to-assign-incoming-request-body-datastring-to-a/m-p/3286054#M1208015</link>
      <description>&lt;P&gt;I highly doubt that has anything to do with JS, and would look at how SN implemented it and how it's data type is set within SN's Java code for interpreting the data type. Because if the variable is named and detailed by SN as a String and it's not returned as one, then that's where the problem lies, not the language.&lt;BR /&gt;&lt;BR /&gt;It should be raised as a SN defect, as it should NOT clear the 'request.body.data' object either when JSON parsing 'dataString', which it does, meaning that it's being set by reference somehow. Thus exposing the coding error on SN side.&lt;/P&gt;</description>
      <pubDate>Wed, 11 Jun 2025 01:12:13 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/why-do-i-have-to-assign-incoming-request-body-datastring-to-a/m-p/3286054#M1208015</guid>
      <dc:creator>scottl</dc:creator>
      <dc:date>2025-06-11T01:12:13Z</dc:date>
    </item>
  </channel>
</rss>

