How to I return JSON from a Google Cloud Function

11,065

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)
Share:
11,065
Dustin Ingram
Author by

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, 2022

Comments

  • Dustin Ingram
    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's text/html instead).

  • Oxydron
    Oxydron over 4 years
    Python 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
    Dustin Ingram over 4 years
    @Oxydron I think you missed the last example I have here.
  • Oxydron
    Oxydron over 4 years
    Indeed 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
    daramcq over 4 years
    I'd suggest adding the dash into 'Content-Type' for the second part of your answer.
  • John Carter
    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
    Dustin Ingram about 4 years
    Did you import json? When you say it didn't work, what error did you get?
  • daniel rocha
    daniel rocha about 4 years
    Yes, 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.