Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

JsonResult(object) causes "The collection type 'Newtonsoft.Json.Linq.JToken' is not supported."

Ask Question

I upgraded an existing ASP.NET Core 2.2 project to 3.0. I have a method that returns JSON which worked in 2.2, but in 3.0, it causes "The collection type 'Newtonsoft.Json.Linq.JToken' is not supported." at the return.

[HttpGet()]
public async Task<JsonResult> Get()
    var res = some class object in a third-party library;
    return new JsonResult(res);

I have searched Google and found that Microsoft has replaced Newtonsoft Json to System.Text.Json, but I have not explicitly used Newtonsoft Json. In the project's "Frameworks", I could see the Newtonsoft Json, and I removed it and the using Newtonsoft.Json.Linq statements, but the result was the same. How not to use Newtonsoft Json?

The error message is:

System.NotSupportedException: The collection type 'Newtonsoft.Json.Linq.JToken' is not supported.
   at System.Text.Json.JsonClassInfo.GetElementType(Type propertyType, Type parentType, MemberInfo memberInfo, JsonSerializerOptions options)
   at System.Text.Json.JsonClassInfo.CreateProperty(Type declaredPropertyType, Type runtimePropertyType, Type implementedPropertyType, PropertyInfo propertyInfo, Type parentClassType, JsonConverter converter, JsonSerializerOptions options)
   at System.Text.Json.JsonClassInfo.AddProperty(Type propertyType, PropertyInfo propertyInfo, Type classType, JsonSerializerOptions options)
   at System.Text.Json.JsonClassInfo.AddPolicyProperty(Type propertyType, JsonSerializerOptions options)
   at System.Text.Json.JsonClassInfo..ctor(Type type, JsonSerializerOptions options)
   at System.Text.Json.JsonSerializerOptions.GetOrAddClass(Type classType)
   at System.Text.Json.JsonPropertyInfo.get_ElementClassInfo()
   at System.Text.Json.JsonSerializer.HandleObject(JsonPropertyInfo jsonPropertyInfo, JsonSerializerOptions options, Utf8JsonWriter writer, WriteStack& state)
   at System.Text.Json.JsonSerializer.WriteObject(JsonSerializerOptions options, Utf8JsonWriter writer, WriteStack& state)
   at System.Text.Json.JsonSerializer.Write(Utf8JsonWriter writer, Int32 originalWriterDepth, Int32 flushThreshold, JsonSerializerOptions options, WriteStack& state)
   at System.Text.Json.JsonSerializer.WriteAsyncCore(Stream utf8Json, Object value, Type inputType, JsonSerializerOptions options, CancellationToken cancellationToken)
   at Microsoft.AspNetCore.Mvc.Infrastructure.SystemTextJsonResultExecutor.ExecuteAsync(ActionContext context, JsonResult result)
   at Microsoft.AspNetCore.Mvc.Infrastructure.SystemTextJsonResultExecutor.ExecuteAsync(ActionContext context, JsonResult result)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeResultAsync>g__Logged|21_0(ResourceInvoker invoker, IActionResult result)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResultFilterAsync>g__Awaited|29_0[TFilter,TFilterAsync](ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResultExecutedContextSealed context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.ResultNext[TFilter,TFilterAsync](State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeResultFilters()
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|19_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Logged|17_1(ResourceInvoker invoker)
   at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
   at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

PS: I have tried it with other types and got some similar exception messages for collection types. With that, I searched Google and found this open issue on .NET Core's GitHub. It seems like System.Text.JSon currently does not support collection types fully, and a workaround is just use the old Newtonsoft Json.

  • Add a package reference to Microsoft.AspNetCore.Mvc.NewtonsoftJson

  • Update Startup.ConfigureServices to call AddNewtonsoftJson: services.AddMvc().AddNewtonsoftJson();

  • Thanks, but is there any way not to use the Newtonsoft Json and use the new System.Text.Json? All I want is just to send the object in JSON as the response. Surely, the Microsoft's new equivalent class could do that too, right? If there is no such feature, I will use your solution and mark it as the answer. – Damn Vegetables Sep 24, 2019 at 7:15 It seems like a limitation of the new Json (I added the details at the bottom of the OP). I'' just use the old one. – Damn Vegetables Sep 24, 2019 at 9:59 I had no other option but to use existing NewtonsoftJson. I have tested it and upper solution works without any problems. – sabiland Sep 24, 2019 at 10:38 A Huge thanks to you for providing this. I was madly searching for this. And most importantly it works. – Sajeeb Chandan Saha Jan 9, 2020 at 8:28 Anecdotally, the Newtonsoft is more robust and faster than SystemText. Make sure to pick the package version that is compatible with your dotNet. (use the nuget manager) This answer worked for me! thanks. – John Henckel Apr 21, 2023 at 16:56

    In .NET Core 3.1 based web applications if you are getting this error, the Nuget package you need to have is the same "Microsoft.AspNetCore.Mvc.NewtonsoftJson".

    The code in the ConfigureServices method in the Startup class would look like

    services.AddControllersWithViews().AddNewtonsoftJson();
    

    Refer this link

    Note: the using in a controller is using Newtonsoft.Json; and if needed using Newtonsoft.Json.Linq; ( migrating from core 2.1 to 3.1 ) – cmill Apr 17, 2020 at 19:19 I need return dictionary to view with ajax but i getting this error. Thank you!!! this work for me .Net core 3.1 – A3IOU Nov 25, 2020 at 10:39

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.