Hướng dẫn json response flask python
I have a function that analyzes a CSV file with Pandas and produces a dict with summary information. I want to return the results as a response from a Flask view. How do I return a JSON response?
davidism 113k24 gold badges367 silver badges323 bronze badges asked Oct 26, 2012 at 5:56
0 As of Flask 1.1.0 a view can directly return a Python dict and Flask will call
If your Flask version is less than 1.1.0 or to return a different JSON-serializable object, import and use
answered Oct 26, 2012 at 15:33
codegeekcodegeek 30.7k11 gold badges62 silver badges61 bronze badges 0
davidism 113k24 gold badges367 silver badges323 bronze badges answered Nov 16, 2014 at 20:16
sclsscls 15.4k9 gold badges42 silver badges52 bronze badges 0 Pass keyword arguments to
If you already have a dict, you can pass it directly as
davidism 113k24 gold badges367 silver badges323 bronze badges answered Oct 26, 2012 at 6:11
zengrzengr 37.7k37 gold badges125 silver badges190 bronze badges 0 If you don't want to use
davidism 113k24 gold badges367 silver badges323 bronze badges answered Jan 23, 2018 at 19:54
0 To return a JSON response and set a status code you can use
Inspiration taken from this comment in the Flask issue tracker.
davidism 113k24 gold badges367 silver badges323 bronze badges answered May 22, 2019 at 22:02
roublerouble 14.8k16 gold badges101 silver badges97 bronze badges If you want to analyze a file uploaded by the user, the Flask quickstart shows how to get files from users and access them. Get the file from
Replace the
davidism 113k24 gold badges367 silver badges323 bronze badges answered Dec 5, 2013 at 16:44
teechapteechap 7716 silver badges6 bronze badges Flask 1.1.x supports returning a JSON dict without calling
is equivalent to
See the pull request that added this: https://github.com/pallets/flask/pull/3111
davidism 113k24 gold badges367 silver badges323 bronze badges answered May 14, 2020 at 8:32
sarfarazsajjadsarfarazsajjad 1,1072 gold badges16 silver badges30 bronze badges 0 I use a decorator to return the result of
davidism 113k24 gold badges367 silver badges323 bronze badges answered Jun 10, 2016 at 13:37
imaniman 20k8 gold badges30 silver badges31 bronze badges 0 Prior to Flask 0.11,
davidism 113k24 gold badges367 silver badges323 bronze badges answered Jun 26, 2016 at 15:42
0 In Flask 1.1, if you return a dictionary and it will automatically be converted into JSON. So if
The
SO that asks about including the status code was closed as a duplicate to this one. So to also answer that question, you can include the status code by returning a tuple of the form
You can check the status code using
answered Jan 3, 2020 at 14:37
Steven C. HowellSteven C. Howell 15.1k14 gold badges70 silver badges87 bronze badges The answer is the same when using Flask's class-based views.
davidism 113k24 gold badges367 silver badges323 bronze badges answered Oct 24, 2019 at 11:40
if its a dict, flask can return it directly (Version 1.0.2)
answered Dec 3, 2019 at 14:15
c8999c 3f964f64c8999c 3f964f64 1,2761 gold badge8 silver badges22 bronze badges To serialize an object, use answered Nov 12, 2021 at 10:49
bkoiki950bkoiki950 1871 silver badge3 bronze badges 1 I like this way:
answered Sep 16, 2020 at 13:58
Amin ShojaeiAmin Shojaei 4,3502 gold badges33 silver badges41 bronze badges 1 Not the answer you're looking for? Browse other questions tagged python json flask or ask your own question. |