How to I return JSON from a Google Cloud Function
Solution 1
Cloud Functions has Flask available under the hood, so you can use it's jsonify
function to return a JSON response.
In your function:
from flask import jsonify
def my_function(request):
data = ...
return jsonify(data)
This will return a flask.Response
object with the application/json Content-Type
and your data
serialized to JSON.
You can also do this manually if you prefer to avoid using Flask:
import json
def my_function(request):
data = ...
return json.dumps(data), 200, {'Content-Type': 'application/json'}
Solution 2
You don't need Flask per se
import json
def my_function(request):
data = ...
return json.dumps(data), 200, {'ContentType': 'application/json'}
Make 200 whatever response code is suitable, eg. 404, 500, 301, etc.
If you're replying from a HTML AJAX request
return json.dumps({'success': True, 'data': data}), 200, {'ContentType': 'application/json'}
to return an error instead for the AJAX request
return json.dumps({'error': True}), 404, {'ContentType': 'application/json'}
Solution 3
For me, json.dumps() did not work in the cloud function. It worked only in my local server. So, I had to build the json by myself:
headers= {
'Access-Control-Allow-Origin': '*',
'Content-Type':'application/json'
}
id1= "1234567"
var1= "variable 1"
text = '{"id1":"'+id1+'","var1":"'+var1+'"}'
return (text, 200, headers)
Dustin Ingram
I’m Dustin (aka @di), a Developer Advocate at Google, focused on supporting the Python community on the Google Cloud Platform. I’m also a director of the Python Software Foundation, maintainer of PyPI, organizer for the PyTexas conference and have a master’s degree in Computer Science from Drexel University.
Updated on June 25, 2022Comments
-
Dustin Ingram almost 2 years
How do I return JSON from a HTTP Google Cloud Function in Python? Right now I have something like:
import json def my_function(request): data = ... return json.dumps(data)
This correctly returns JSON, but the
Content-Type
is wrong (it'stext/html
instead). -
Oxydron over 4 yearsPython has a standard library to deal with json (json.dumps), no need of an external library to do this. In some cases, a simple str(data) do the trick.
-
Dustin Ingram over 4 years@Oxydron I think you missed the last example I have here.
-
Oxydron over 4 yearsIndeed I missed :\, but I think that the only correct answer is that last one you written, because it solves the problem without external libraries.
-
daramcq over 4 yearsI'd suggest adding the dash into 'Content-Type' for the second part of your answer.
-
John Carter over 4 years@Oxydron since as Dustin points out, Flask is baked into gcloud functions, I'd argue this doesn't really add a dependency (eg request is a
flask.Request
object), it seems reasonable to make use of the available functionality. Adding flask to requests isn't actually needed for their example to work, though being explicit about your dependencies is reasonable. -
Dustin Ingram about 4 yearsDid you
import json
? When you say it didn't work, what error did you get? -
daniel rocha about 4 yearsYes, I imported and tried both json and flask.jsonify. It returned an error "INVALID ARGUMENT" but I was not able to get any information beyond this. I know it was on this line because if I remove it from the code, the error message does not appear.