<?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>topic JSON.parse fails in post executions script even with valid JSON in Synthetic Monitoring</title>
    <link>https://community.dynatrace.com/t5/Synthetic-Monitoring/JSON-parse-fails-in-post-executions-script-even-with-valid-JSON/m-p/198179#M1548</link>
    <description>&lt;P&gt;We have a HTTP monitor and in the post-execution script of one of the requests, we do a simple response validation.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;var responseBody = response.getResponseBody();&lt;BR /&gt;var jsonData = JSON.parse(responseBody);&lt;BR /&gt;if((jsonData.value.length == 0)) {&lt;BR /&gt;api.fail("Unexpected response");&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However this step consistently fails. I have extracted the response from the monitor execution details ad tried to parse in JS runtime(chrome, nodejs) and there JSON.parse works without any problems. I am not sure what is causing this problem on DT as especially in the event section, I only see "Script execution failed" without further context.&lt;/P&gt;</description>
    <pubDate>Fri, 11 Nov 2022 08:42:16 GMT</pubDate>
    <dc:creator>Prithvi</dc:creator>
    <dc:date>2022-11-11T08:42:16Z</dc:date>
    <item>
      <title>JSON.parse fails in post executions script even with valid JSON</title>
      <link>https://community.dynatrace.com/t5/Synthetic-Monitoring/JSON-parse-fails-in-post-executions-script-even-with-valid-JSON/m-p/198179#M1548</link>
      <description>&lt;P&gt;We have a HTTP monitor and in the post-execution script of one of the requests, we do a simple response validation.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;var responseBody = response.getResponseBody();&lt;BR /&gt;var jsonData = JSON.parse(responseBody);&lt;BR /&gt;if((jsonData.value.length == 0)) {&lt;BR /&gt;api.fail("Unexpected response");&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However this step consistently fails. I have extracted the response from the monitor execution details ad tried to parse in JS runtime(chrome, nodejs) and there JSON.parse works without any problems. I am not sure what is causing this problem on DT as especially in the event section, I only see "Script execution failed" without further context.&lt;/P&gt;</description>
      <pubDate>Fri, 11 Nov 2022 08:42:16 GMT</pubDate>
      <guid>https://community.dynatrace.com/t5/Synthetic-Monitoring/JSON-parse-fails-in-post-executions-script-even-with-valid-JSON/m-p/198179#M1548</guid>
      <dc:creator>Prithvi</dc:creator>
      <dc:date>2022-11-11T08:42:16Z</dc:date>
    </item>
    <item>
      <title>Re: JSON.parse fails in post executions script even with valid JSON</title>
      <link>https://community.dynatrace.com/t5/Synthetic-Monitoring/JSON-parse-fails-in-post-executions-script-even-with-valid-JSON/m-p/198261#M1549</link>
      <description>&lt;P&gt;hi prithvi&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;to be clear. when you do a JSON.parse of the Body, you expect a value with some number&amp;nbsp;&lt;/P&gt;&lt;P&gt;i will assume this number as a int,&amp;nbsp; and will give to this number n value. in this theory will be 1000&lt;/P&gt;&lt;P&gt;so:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;value = 1000&lt;/LI-CODE&gt;&lt;P&gt;this raise a error in javascript because you are using the function lenght. and this function only count strings when you do something like&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;value.lenght

this will be undefined&lt;/LI-CODE&gt;&lt;P&gt;you have two options:&lt;/P&gt;&lt;P&gt;1 ) you can use toString hoping that it works will be sometimes it doesnt work&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;value.toString().lenght = 4 &lt;/LI-CODE&gt;&lt;P&gt;2) you can use a side function inside the postscript to calculate the lenght something like&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;var nLength = function(n) {
return (Math.log(Math.abs(n)+1) * 0.43429448190325176 | 0) + 1;
}

nLength(value) = 4&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;for future test remember you can allways use the error to see the full body in the response to know if the problem its for the logic or for the values in the body&lt;/P&gt;&lt;P&gt;some aproach:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;var responseBody = response.getResponseBody();
var jsonData = JSON.parse(responseBody);
var datastringify = JSON.stringify(jsonData);
if (response.getStatusCode() != 200) {
    api.fail("HTTP error:" + datastringify );
} &lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 13 Nov 2022 07:16:45 GMT</pubDate>
      <guid>https://community.dynatrace.com/t5/Synthetic-Monitoring/JSON-parse-fails-in-post-executions-script-even-with-valid-JSON/m-p/198261#M1549</guid>
      <dc:creator>rgarzon1</dc:creator>
      <dc:date>2022-11-13T07:16:45Z</dc:date>
    </item>
  </channel>
</rss>

