Azure functions: return JSON object

Viewed 17986

Consider:

import logging
import azure.functions as func

def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    name = {"test":"jjj"}
    return func.HttpResponse(name)

Above is my Azure function (V2) using Python preview.

If I return

func.HttpResponse(f"{name}")

it works, but if I return a dict object it does not.

The error displayed is:

Exception: TypeError: reponse is expected to be either of str, bytes, or bytearray, got dict

6 Answers

You need to convert your dictionary to a JSON string using the built-in JSON library - json.dumps.

Then you can set the MIME type (Content-Type) of your function to application/json. By default Azure functions HttpResponse returns text/plain Content-Type.

import json
import logging
import azure.functions as func

def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    name = {"test":"jjj"}
    return func.HttpResponse(
        json.dumps(name),
        mimetype="application/json",
    )

Better way:

func.HttpResponse.mimetype = 'application/json'
func.HttpResponse.charset = 'utf-8'

return func.HttpResponse(json_object)

First of all, I am not a Python expert. I am just trying to let you know what the issue is all about.

So on Azure function if you look, it would seem its return type is IActionResult. If you decompile it, you would see:

IActionResult IConvertToActionResult.Convert()
    {
      ActionResult result = this.Result;
      if (result != null)
        return (IActionResult) result;
      return (IActionResult) new ObjectResult((object) this.Value)
      {
        DeclaredType = typeof (TValue)
      };
    }

So it expects an object from you rather than a Dictionary or List or any kind of generics type. But if you convert it into an object like OkObjectResult, you wouldn't encounter any compile error. See the example below:

IDictionary<int, string> MyDictionary = new Dictionary<int, string>();
MyDictionary.Add(new KeyValuePair<int, string>(1, "One"));
MyDictionary.Add(new KeyValuePair<int, string>(2, "Two"));

// As we have to return IAction Type, so converting to IAction class
// using OkObjectResult we can even use OkResult
return new MyDictionary;

The above code would encounter your compile error. Because it does not support Dictionary or List Or any generic directly. See the screenshot below:

Enter image description here

See what the error said:

Enter image description here

But if you convert your Dictionary or List Or any generic into an object type, it will resolve your issue for sure. See the below example:

IDictionary<int, string> MyDictionary = new Dictionary<int, string>();
MyDictionary.Add(new KeyValuePair<int, string>(1, "One"));
MyDictionary.Add(new KeyValuePair<int, string>(2, "Two"));

// So have converted MyDictionary into Object type that the compiler deserve. And no compile error encountered.
return new OkObjectResult(MyDictionary);

See the screenshot below:

Enter image description here

Note: If you try to parse your func.HttpResponse(name) into an object then finally return, your problem should resolve. So you can try to add a reference to this package in your project (import json) and try the below code:

import json

name = {"test":"jjj"}

print(json.dumps(name))

The proper way to do this is:

return func.HttpResponse(
   body = name,
   status_code = 200
)

See the documentation for more information on the HttpResponse class.

Try it like this:

 import json

     return func.HttpResponse (
        json.dumps({
        'lat': lat,
        'lon': lon

        })
     )

what about this, create a json object (json dumps creates a json from a dictonary) and return that in the body of your response:

# json object
result = {
    "arg1" : variable1,
    "arg2" : variable2
}
result = json.dumps(result, indent = 4) 

return func.HttpResponse(
        body = result,
        status_code=200
)
Related