1
@app.route("/data/<dataType>", methods=['GET'])
def realtime(dataType):
    _data = None
    with open(os.path.join("data/"+ dataType +".json")) as f:
    _data = json.loads(f)
return dumps(_data)

I wrote this code to get data in JSON format from a file, but it keeps giving me the error:

TypeError: expected string or buffer

What is wrong with it?

3
  • loads expects a string with json, not a file object. Use load instead Commented Jul 4, 2015 at 17:33
  • Additionally, your indenting is very incorrect in the posted code. Commented Jul 4, 2015 at 17:38
  • yeah, it was only on the stack, i made some indent while uploading the question. my original code is ok . thx anyway Commented Jul 4, 2015 at 17:45

1 Answer 1

2

f is a file, while json.loads() expects a string. Use f.read() to read a string from the file.

Sign up to request clarification or add additional context in comments.

2 Comments

To clarify, f.read() reads the entire file as a string. So Stephen probably wants json.loads(f.read()).
Be sure to ... Oh, he's gone.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.