0

I have a json file that I provide via flask, and I'd like to parse it from a requests object, but I can't get the full data back. I get a broken json file. What I get is about one third of the way through the file to the end.

I get different answers whether I use r.json() or json.loads(r.text)from the simple get request:

r = requests.get("url/api")

If I stream it manually, I can see the start of the file

r= requests.get("url/api", stream=True)
r.raw.read(1000).decode('utf-8')

And I successfully save down the file as here how to parse big requests object, but the problem persists when I try to then upload the file using json.load(local_filename)

The shape of my json file is:

[{"key1":"value1_1", "key2":"value2_1"},{"key1":"value1_2", "key2":"value2_2"}.......]

This is how I provide the json from flask

@app.route('/api', methods=['GET'])
def provide_json():
    with open('record_file.json') as f:
        record_file = json.load(f)
    return json.dumps(record_file)

I've checked that my json file is valid, and I'm also assuming that my flask code with throw an exception is I wasn't delivering a valid json file because I've used json.dumps. Is that right? I've also tried wrapping the json file in flask with return json.dumps({"data_file":record_file}) but it doesn't change the problem.

1 Answers1

0

You are returning list which is not a valid response for flask. Try this

from flask import jsonify
@app.route('/api', methods=['GET'])
def provide_json():
    with open('record_file.json') as f:
        record_file = json.load(f)
    return jsonify({"data": record_file})
jen
  • 199
  • 1
  • 5
  • Thanks, but unfortunately, the problem persists. I did note that I had made wrapped the list in a {}, so your suggestion is a change from providing via jsonify instead of json.dumps, but the problem persists. It feels like this isn't a problem at the end of providing the file. – Cormac Hollingsworth Aug 13 '20 at 08:22