title: How to speed up returning a 20MB Json file from a Python-Flask application?
body: <p>I am trying to call an API which in turn triggers a store procedure from our sqlserver database. This is how I coded it.</p>
  <pre><code>class Api_Name(Resource):
  
      def __init__(self):
          pass
  
      @classmethod
      def get(self):
          try:
              engine = database_engine
              connection = engine.connect()
              sql = &quot;DECLARE @return_value int EXEC @return_value = [dbname].[dbo].[proc_name])
              return call_proc(sql, apiname, starttime, connection)
          except Exception as e:
              return {'message': 'Proc execution failed with error =&gt; {error}'.format(error=e)}, 400
          pass
  </code></pre>
  <p><code>call_proc</code> is the method where I return the JSON from database.</p>
  <pre><code>def call_proc(sql: str, connection):
      try:
          json_data = []
          rv = connection.execute(sql)
          for result in rv:
              json_data.append(dict(zip(result.keys(), result)))
          return Response(json.dumps(json_data), status=200)
      except Exception as e:
          return {'message': '{error}'.format(error=e)}, 400
      finally:
          connection.close()
  </code></pre>
  <p>The problem with the output is the way JSON is returned and the size of it.
  At first the API used to take 1minute 30seconds: when the return statement was like this:</p>
  <pre><code>case1: return Response(json.dumps(json_data), status=200, mimetype='application/json')
  </code></pre>
  <p>After looking online, I found that the above statement is trying to prettify JSON. So I removed <code>mimetype</code> from the response &amp; made it as</p>
  <pre><code>case2: return Response(json.dumps(json_data), status=200)
  </code></pre>
  <p>The API runs for 30seconds, although the JSON output is not aligned properly but its still JSON.
  I see the output size of the JSON returned from the API is close 20MB. I observed this on postman response:</p>
  <pre><code>Status: 200 OK    Time: 29s    Size: 19MB
  </code></pre>
  <p>The difference in Json output:</p>
  <p>case1:</p>
  <pre><code>[   {
          &quot;col1&quot;:&quot;val1&quot;,
          &quot;col2&quot;:&quot;val2&quot;
      },
      {
          &quot;col1&quot;:&quot;val1&quot;,
          &quot;col2&quot;:&quot;val2&quot;
      }
  ]
  </code></pre>
  <p>case2:</p>
  <pre><code>[{&quot;col1&quot;:&quot;val1&quot;,&quot;col2&quot;:&quot;val2&quot;},{&quot;col1&quot;:&quot;val1&quot;,&quot;col2&quot;:&quot;val2&quot;}]
  </code></pre>
  <p>Will the difference in output from the two aforementioned cases are different ? If so, how can I fix the problem ?
  If there is no difference, is there any way I speed up this further and reduce the run time further more,  like compressing the JSON which I am returning ?</p>