Home » Return JSON from C# Azure function

Return JSON from C# Azure function

In this blog post I’ll build a simple C# Azure function that returns an object as JSON. That’s useful if you want to build a simple “API” or if you just want to return some information in a structured format. Such a function could read data from an on-premise environment and provide this data to a logic app, because it’s much easier to connect an Azure function to on-premise than a logic app.

Create a C# Azure Function

First step is to create a new C# function. I’ll use the HttpTriggerWithParameters-CSharp template and I’ll use the authorization level ‘Anonymous’ (that’s okay for this demo):

When the function is created, we can start to implement the logic, but before we jump into the code, we must include the package Newtonsoft.Json. As this package is a standard package and specially handled, it can be referenced by simply putting the following line at the beginning of the run.csx file:

#r "Newtonsoft.Json"

References via #r only works for some specific packages (see: Azure Functions C# developer reference). Other packages must be added by adding/modifying the project.json file. I described it in the blog post Working with Azure functions (part 2 – C#)

The serialization of a C# object works via:

string json = Newtonsoft.Json.JsonConvert.SerializeObject(myObject);
string jsonFormatted = Newtonsoft.Json.JsonConvert.SerializeObject(myObject, Newtonsoft.Json.Formatting.Indented);

C# sample – return JSON from Azure function

The final, very simple, C# sample to return an object as JSON via an Azure function is:

run.csx

#r "Newtonsoft.Json"
using System.Net;
using System.Text;
using Newtonsoft.Json;

public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, string name, TraceWriter log)
{
    Website data = new Website(); // just a sample object
    data.Name = "CodeHollow";
    data.Url = "https://codehollow.com";
    data.Author = "Armin Reiter";
    data.BlogEntries = 36;
    data.Caller = name;

    var json = JsonConvert.SerializeObject(data, Formatting.Indented);
    
    return new HttpResponseMessage(HttpStatusCode.OK) 
    {
        Content = new StringContent(json, Encoding.UTF8, "application/json")
    };
}

public class Website
{
    public string Name { get; set; }
    public string Url { get; set; }
    public string Author { get; set; }
    public int BlogEntries { get; set; }
    public string Caller { get; set; }
}

That’s the whole magic. If we look at the output window of our Azure function, then it should show us the successful compilation and if we started, then also the information that the function started/stopped:

To call the function via an URL, we must get the URL from the function. Fortunately there is a button “Get function URL” which makes it easy:

If we call the URL with, then the function returns JSON content that we can reuse in our applications. I called it with my browser and that’s the output:

{
  "Name": "CodeHollow",
  "Url": "https://codehollow.com",
  "Author": "Armin Reiter",
  "BlogEntries": 36,
  "Caller": "codehollow"
}

But you can also use e.g. the Powershell to read the content:

$response = iwr https://codehollowsample.azurewebsites.net/api/HttpTriggerCSharp/name/codehollow
$data = ConvertFrom-Json $response.Content
$data

Additional information

Azure Functions C# developer reference: https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference-csharp
Return HTML or file content from C# Azure function: https://codehollow.com/2017/02/return-html-file-content-c-azure-function/
Working with Azure functions (part 2 – C#): https://codehollow.com/2016/11/working-with-azure-functions-part-2-c/
Azure Functions – Time Trigger (CRON) Cheat Sheet : https://codehollow.com/2017/02/azure-functions-time-trigger-cron-cheat-sheet/

Leave a Reply

Your email address will not be published. Required fields are marked *