Search code examples
pythonflaskjsonify

I don't know how to select specific items from json object in my web application using flask


response = requests.get(url)
response_json = response.json()
results = response_json['a'][0]
output = results['a_1'] + results['a_2'] + results['a_3']
return jsonify(output)

my output
"abcdefg"

what I want
abcdefg

How should I fix it?


Solution

  • jsonify builds a Flask Response object. With this code you're trying to access it as if it was a dictionary:

    results = jsonify( response_json )["results"][0]  # bad
    

    I think you're looking for:

    results  = jsonify( response_json["results"[0] )
    

    Note that here response_json is actually a python datastructure (a dictionary) because that's what response.json returns.