I have a python function with flask

from flask import jsonify def method1(): result = {"jsonrpc": "2.0", ... } return jsonify(result) 

then, from another method, I call method1:

 def method2(): otherResult = method1() ... 

In method2, I want to get the contents of result (which is from method1). How can I do it? The problem is that if I try to do this:

 response = otherResult['jsonrpc'] 

then at execution the error is received:

 TypeError: 'Response' object has no attribute '__getitem__' 

    1 answer 1

    When you call method1 you are returned a Response object, which contains the transmitted data in the form of bytes . To convert this data into a dictionary, you need to decode them first, and then convert them using the json library.

    For example, like this:

     from flask import Flask from flask import jsonify import json app = Flask(__name__) def method1(): result = {"jsonrpc": "2.0"} return jsonify(result) @app.route("/") def hello(): response = method1() data = response.get_data().decode() dct = json.loads(data) return dct["jsonrpc"] if __name__ == "__main__": app.run() 
    • one
      Still, transfer the data to json and then immediately parse back somehow a little bust - andreymal
    • Yes, in this case it is redundant, but who knows why the author of the question did just that. Maybe there is a logic smarter than we see. - Avernial