Flattening an array in Flask API <p>I'm making an API with flask that takes as a POST message body a nested array, and then returns a list of the values. An example would be curl …. –d ”(([[1, [], [2, 3]], [[4]], 5])” would return [1,2,3,4,5]. The flattening script works in the command line, but when I POST to the API, I get some weird results. Code is here:</p> <pre><code>app = Flask(__name__) app.config.from_object(__name__) app.config.from_envvar('PHIGITAL_SETTINGS', silent=True) @lru_cache(maxsize=500) def flatten(l): flattened = [] for el in l: if isinstance(el, (list, tuple)): flattened.extend(flatten(el)) else: flattened.append(el) return flattened @app.route('/flatten', methods=['POST']) def flatten_api(): if request.method == 'POST': try: return jsonify({"response" : flatten(request.data)}) except Exception as e: return jsonify({"response" : "ERROR: %s" % str(e)}) if __name__ == '__main__': app.run() </code></pre> <p>Testing this in Postman gets me the response: { "response": [ "[", "[", "1", ",", " ", "[", "]", ",", " ", "[", "2", ",", " ", "3", "]", "]", ",", " ", "[", "[", "4", "]", "]", ",", " ", "5", "]" ] }</p> <p>Which is obviously not correct. I thought that it might have to do with the fact that request.data is a string, so I tried using ast.literal_eval to make request.data a list, but then I get the error "unhashable type: 'list'" when I try to call flatten on ast.literal_eval(request.data). I'm totally stumped and any help would be greatly appreciated.</p> <p>Also, is it possible in Flask to return just a value, rather than a key value pair? I'd rather return just [1,2,3,4,5] rather than {"response": [1,2,3,4,5]}</p>