title: How to get raw datetime value from request body in Azure API Management?
body: <p>Currently I'm trying to access the raw value of a JSON property in Azure API Management.
  I have request as follows:</p>
  
  <pre><code>{
     "date": "2019-12-09T12:00:00Z"
  }
  </code></pre>
  
  <p>and an operation like:</p>
  
  <pre class="lang-xml prettyprint-override"><code>    &lt;inbound&gt;
          &lt;base /&gt;
          &lt;!-- Assume datetime in format 2019-12-09T12:00:00Z --&gt;
          &lt;set-variable name="time" value="@(context.Request.Body.As&lt;JObject&gt;()["date"])" /&gt; 
          &lt;!-- Attempt to acquire raw value --&gt;
          &lt;set-variable name="rawValue" value="@{
              var value = JsonConvert.SerializeObject((JValue)context.Variables["date"]);
              return value.ToString();
          }" /&gt;
          &lt;!-- raw value is: \"2019-12-09T12:00:00Z\" --&gt;
      &lt;/inbound&gt;
  </code></pre>
  
  <p>I found a similar answer here:
  <a href="https://stackoverflow.com/questions/35138346/jtoken-get-raw-original-json-value">JToken: Get raw/original JSON value</a>
  so I tried to serialize the object.</p>
  
  <p>But in my case, Azure API Management outputs the raw value with additional quotes like this:</p>
  
  <pre><code>set-variable (0 ms)
  {
      "message": "Context variable was successfully set.",
      "name": "rawValue",
      "value": "\"2019-12-09T12:00:01Z\""
  }
  </code></pre>
  
  <p>A simple way would be to replace them, but isn't there a better solution?</p>
  
  <p>+++ UPDATE +++</p>
  
  <p>Just found this solution:</p>
  
  <pre><code>Convert.ToDateTime(context.Request.Body.As&lt;JObject&gt;()["date"]).ToString("o")
  </code></pre>
  
  <p>which outputs:</p>
  
  <pre><code>set-variable (0 ms)
  {
      "message": "Expression was successfully evaluated.",
      "expression": "Convert.ToDateTime(context.Request.Body.As&lt;JObject&gt;()[\"date\"]).ToString(\"o\")",
      "value": "2019-12-10T09:10:00.0000000Z"
  }
  </code></pre>
  
  <p>IMO this is still not 100% what I expected, but at least it's the right format.
  I'm still open for any better solutions.</p>