TypeError: Expected bytes in Flask app
<p>I am trying to implement a simple flask app which will pass a json file to the front end, but got an error as following:</p>

<pre><code>&gt; 127.0.0.1 - - [04/Oct/2016 17:53:02] "GET /test HTTP/1.1" 500 - Traceback (most recent call last):   File
&gt; "/Users/michelleshieh/anaconda2/lib/python2.7/site-packages/flask/app.py",
&gt; line 2000, in __call__
&gt;     return self.wsgi_app(environ, start_response)   File "/Users/michelleshieh/anaconda2/lib/python2.7/site-packages/flask/app.py",
&gt; line 1992, in wsgi_app
&gt;     return response(environ, start_response)   File "/Users/michelleshieh/anaconda2/lib/python2.7/site-packages/werkzeug/wrappers.py",
&gt; line 1228, in __call__
&gt;     app_iter, status, headers = self.get_wsgi_response(environ)   File "/Users/michelleshieh/anaconda2/lib/python2.7/site-packages/werkzeug/wrappers.py",
&gt; line 1216, in get_wsgi_response
&gt;     headers = self.get_wsgi_headers(environ)   File "/Users/michelleshieh/anaconda2/lib/python2.7/site-packages/werkzeug/wrappers.py",
&gt; line 1167, in get_wsgi_headers
&gt;     for x in self.response)   File "/Users/michelleshieh/anaconda2/lib/python2.7/site-packages/werkzeug/wrappers.py",
&gt; line 1167, in &lt;genexpr&gt;
&gt;     for x in self.response)   File "/Users/michelleshieh/anaconda2/lib/python2.7/site-packages/werkzeug/_compat.py",
&gt; line 112, in to_bytes
&gt;     raise TypeError('Expected bytes') TypeError: Expected bytes
&gt; 127.0.0.1 - - [04/Oct/2016 17:53:03] "GET /favicon.ico HTTP/1.1" 404
</code></pre>

<p>The coding related to the url '/test' is:</p>

<pre><code>@app.route("/test",methods=['GET'])

    def get_local_json():
        SITE_ROOT = os.path.realpath(os.path.dirname(__file__))
        json_url = os.path.join(SITE_ROOT, "static/data","predict.json")
        #console.log("url is all right")
        data = json.load(open(json_url))
        return app.response_class(data, content_type='application/json')
</code></pre>

<p>And the coresponding code for front-end:</p>

<pre><code>$.getJSON('/test', function(data){  
        var myData = [];
        for(var i in data){
            var item = data[i];
            var myItem = [];
            myItem.push(new Date(item.time).getTime());
            myItem.push(item.occupancy);
            myData.push(myItem);
        }
        console.log(myData);
</code></pre>

<p>Any hints are appreciated!!</p>

<p>You are returning a Python object other than a <code>bytes</code> object here:</p>

<pre><code>return app.response_class(data, content_type='application/json')
</code></pre>

<p>That's not a JSON response, that's an <em>unencoded</em> Python list or dictionary.</p>

<p>Just return the JSON data <em>without</em> decoding it:</p>

<pre><code>    with open(json_url, 'rb') as json_file:
        return app.response_class(json_file.read(), content_type='application/json')
</code></pre>

<p>or if you must do something with the data structure first, re-encode it back to JSON. Use the <code>jsonify()</code> utility function for that:</p>

<pre><code>with open(json_url) as json_file:
    data = json.load(json_file)

# manipulate data as needed

return jsonify(data)
</code></pre>

python json flask