What the @ - fixing weirdness in return JSON from Azure functions using F#

Jim Bennett | Jun 19, 2018

I’ve been playing a lot with F# recently, both to build Xamarin apps using Elmish.XamarinForms and for some Azure Functions. Whilst building an HTTP trigger I came across some weirdness when serializing a record type to JSON.

This is the relevant parts of my code:

type Output = { TotalBalance : float }

let Run(req: HttpRequestMessage, boundTable: IQueryable<Transaction>, log: TraceWriter) =
    async {
        // stuff
        return req.CreateResponse(HttpStatusCode.OK, { TotalBalance = sum })
    } |> Async.RunSynchronously

Now it’s normal to assume the Output type would be serialized to JSON correctly, leading to something like:

  "balance" : 100

Unfortunately not - what I actually get is:

  "Balance@" : 100

Note the weird @ symbol on the property name.

So I went spelunking around Google and SO for a fix. The first suggestion was to add the [<CLIMutable>] attribute to my Output type, but this didn’t actually work, the @ symbol was still there.

In the end I found the fix here: https://stackoverflow.com/questions/43118406/return-an-f-record-type-as-json-in-azure-functions/48718297#48718297. You can pass a different JSON formatter to the CreateResponse call, and configure that to return the correct property names:

let jsonFormatter = Formatting.JsonMediaTypeFormatter()
jsonFormatter.SerializerSettings.ContractResolver <- CamelCasePropertyNamesContractResolver()

return req.CreateResponse(HttpStatusCode.OK, { TotalBalance = sum }, jsonFormatter)