4

I'm trying to return JArray object from Azure Function of HttpTrigger type:

JArray a = JArray.Parse("[{\"reportId\": \"1111\",\"reportName\": \"AAAA\"}]");
return req.CreateResponse(HttpStatusCode.OK, a);

However, for some reason, the response body returned as:

[{"reportId":[],"reportName":[]}]

What am I doing wrong?

Boris Lipschitz
  • 9,236
  • 5
  • 53
  • 63

2 Answers2

3

I can repro the issue with Newtonsoft.Json version 10+. If downgrade is possible, please have a try to use Newtonsoft.Json version 9.0.1. Then it works correctly on my side. We also could rasie an issue to Azure function team.

Tom Sun - MSFT
  • 24,161
  • 3
  • 30
  • 47
2

Azure Functions requires that you use Newtonsoft.Json version 9.0.1, as we don't support binding redirects. Your code is running in the same process as the Functions host, which means you have the same binding redirects.

We're improving the Visual Studio experience so that it's clear that there's a strict upper bound on the dependency.

lindydonna
  • 3,874
  • 17
  • 26
  • I understand that if we'll get this error if we use a newer version than what Functions requires. What happens if we use Newtonsoft.Json version 9.0.1 and in the next release you switch to ver. 10+. Do we then need to update our dependency to stay align with what the runtime is using? – Andy T Aug 10 '17 at 22:28
  • Thanks Donna for responding. Currently, when you create a new Functions project using VS 2017 Preview plugin, it sets it up with Newtonsoft.Json 10.0.2. (Yeah, I know the consequences of using the preview software :D ) I wonder what was the breaking change in Json.Net 10.0.0 that was causing this behaviour. – Boris Lipschitz Aug 11 '17 at 01:42
  • @BorisLipschitz That's shouldn't be happening! What version of the Functions extension are you using? – lindydonna Aug 11 '17 at 08:33
  • @AndrésNava-.NET It would be in the next major version of the Functions runtime (2.0) and you'd have to explicitly opt in. There will be other breaking changes in 2.0. – lindydonna Aug 11 '17 at 08:35
  • 1
    @lindydonna-msft I was using the latest version last week: 15.0.30728.0. But today there is a completely new version of Azure Functions plugin. It only has one nuget dependency: Microsoft.NET.Sdk.Functions (1.0.0), which references Newtonsoft.Json (9.0.1). So, all good! Thanks! – Boris Lipschitz Aug 14 '17 at 00:12
  • @BorisLipschitz Great, that's why we added that dependency, makes things less error prone. If my post was helpful, please accept it as the answer so that it helps others. – lindydonna Aug 14 '17 at 11:15
  • 1
    Thankyou @lindydonna-msft - I am working around by serializing the object and returning it in a StringContent. Would you expect this to have broadly the same memory/CPU performance as the default serialization? – Iain Nov 14 '18 at 23:12