title: Fast API : How to return a str as JSON
body: <p>In python, using Fast API, I have a str that when print show (this is an example, the real str is more complex) :</p>
  <pre class="lang-none prettyprint-override"><code>[1592494390, 'test', -0.2761097089544078, -0.0852381808812182, -0.101153, nan]
  </code></pre>
  <p>I want to return this using Fast API as a JSON array.</p>
  <p><strong>Using JSONResponse</strong></p>
  <pre class="lang-py prettyprint-override"><code>def get_json(dataset: str, timeseries: str):
  
      test = &quot;[1592494390, 'test', -0.2761097089544078, -0.0852381808812182, -0.101153, nan]&quot;
      print(test)
      return JSONResponse(content=test)
  </code></pre>
  <p>The print is as expecting showing:</p>
  <pre class="lang-none prettyprint-override"><code>[1592494390, 'test', -0.2761097089544078, -0.0852381808812182, -0.101153, nan]
  </code></pre>
  <p>But the answer of the API when hitting the call is:</p>
  <pre class="lang-none prettyprint-override"><code>&quot;[1592494390, 'test', -0.2761097089544078, -0.0852381808812182, -0.101153, nan]&quot;
  </code></pre>
  <p>So my <code>str</code> is being serialised again and I don't know how to by-pass that.</p>
  <p><strong>Using Response :</strong></p>
  <p>The documentation of Fast API includes a page that describes how to return a Response directly (<a href="https://fastapi.tiangolo.com/advanced/response-directly/" rel="nofollow noreferrer">https://fastapi.tiangolo.com/advanced/response-directly/</a>) where it is written:</p>
  <blockquote>
  <p>When you return a Response directly its data is not validated, converted (serialized), nor documented automatically.</p>
  </blockquote>
  <p>But using this method leads to an error :</p>
  <pre class="lang-py prettyprint-override"><code>def get_json(dataset: str, timeseries: str):
  
      test = &quot;[1592494390, 'test', -0.2761097089544078, -0.0852381808812182, -0.101153, nan]&quot;
      print(test)
      return Response(content=test, media_type=&quot;application/json&quot;)
  </code></pre>
  <pre class="lang-none prettyprint-override"><code>&gt; line 53, in get_json
      return Response(content=test, media_type=&quot;application/json&quot;)
    File &quot;pydantic/main.py&quot;, line 331, in pydantic.main.BaseModel.__init__
  pydantic.error_wrappers.ValidationError: 2 validation errors for Response
  description
    field required (type=value_error.missing)
  content
    value is not a valid dict (type=type_error.dict)
  </code></pre>
  <p>By the way the exact xml example of the documentation gives the same error:</p>
  <pre class="lang-none prettyprint-override"><code>&gt; line 61, in get_json
      return Response(content=data, media_type=&quot;application/xml&quot;)
    File &quot;pydantic/main.py&quot;, line 331, in pydantic.main.BaseModel.__init__
  pydantic.error_wrappers.ValidationError: 2 validation errors for Response
  description
    field required (type=value_error.missing)
  content
    value is not a valid dict (type=type_error.dict)
  </code></pre>
  <p>I know I can convert my data to a an array or dict to be serialized I how want but as I already have the right str and don't want the job to be done several times.</p>