1

I am building a minimal ASP.NET Core Web API. One of my endpoints needs to return data contained in a JObject. This JObject represents a complex nested data structure. What I am finding is that the response from this endpoint loses all of the nested data from the JObject.

From what have read, I suspect this is caused by the fact that by default .NET Core uses its own Json libraries for serialisation and JObect is a Newtonsoft class, but so far I haven't discovered what the best practice is for handling this.

For example, the JObject that I am working with correctly contains all expected key-value data. Some values contain nested data themselves.

If I run .ToString() on it this is what it looks like:

{
  "key": "stringValue",
  "key": "stringValue",
  "key": "stringValue",
  "key": {
    "nestedKey": "nestedValue",
    "nestedKey": "nestedValue"
  },
  "key": "stringValue",
  "key": "stringValue"
  ..
}

The problem is that when I return the JObject like this:

JObject result = service.ParseToJObject(request.Message);
return TypedResults.Ok(result);

The response object looks like this - all the nested values have been lost.

{
  "key1": [],
  "key2": [],
  "key2": [],
   ...
}

I can resolve this issue with the following change (converting the JObject to a string), but want to know if there is a better way to fix this at the global level. With this change the returned object correctly contains all key-value pairs.

string result = service.ParseToJObject(request.Message).ToString();
return TypedResults.Content(result, "application/json");

I've tried various suggestions off the net around setting Json serialization options on the builder (one example below), but none of these have made any difference to the return data.

builder.Services.ConfigureHttpJsonOptions(options =>
{
    options.SerializerOptions.ReferenceHandler = ReferenceHandler.Preserve;
    options.SerializerOptions.PropertyNameCaseInsensitive = true;
    options.SerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
    options.SerializerOptions.Converters.Add(new JsonStringEnumConverter());
});

Thanks

4
  • what is this service.ParseToJObject()? Is it a function that you wrote? Can you imagine that it has a bug? Can you please rewrite your question to be more specific. The input data; the code; the output, and what you expect output to be. Don't leave anything to imagination - but leave out irrelevant information (for example, the fact that you are developing Minimal APIs is most likely to be irrelevant). Good luck Commented Jul 10, 2024 at 0:53
  • I've expanded the question somewhat, let me know if it's still unclear. The operation of service.ParseToObject() shouldn't have any relevance, aside from the fact it returns a JObject which contains key-value pairs. The JObject itself is not in question - if I view it in debugger or print it to console it is correct (I describe its structure above). The problem is that I want to return this JObject to the caller of the API but doing this results in a loss of nested data. Commented Jul 10, 2024 at 1:36
  • even more reasons to take out everything irrelevant... but what is the caller? another program on the server? TypeScript framework in the browser? Try to think what would be the questions somebody who is unfamiliar with your code asks Commented Jul 10, 2024 at 4:58
  • If for whatever reason you must use JObject with System.Text.Json, see How can I serialize a Newtonsoft JToken to JSON using System.Text.Json? for a converter. Better to switch to JsonNode though. Commented Jul 10, 2024 at 22:58

1 Answer 1

1

The idiomatic way here is to just switch from using JObject, Minimal APIs do not work with Newtonsoft's Json.NET, they work only with System.Text.Json and this is not configurable and System.Text.Json does not know anything about JObject.

You have several options here:

  1. Switch to the JsonNode API provided by the System.Text.Json which provides similar capabilities
  2. Write/find custom serializer for System.Text.Json which will handle JObject correctly
  3. Jump some hoops trying to cram in the Json.NET into Minimal APIs - see How to configure NewtonsoftJson with Minimal API in .NET 6.0
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. Will look into switching to JsonNode.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.