Resolved: How to convert object to Json in .net core 6 JsonConverter

In this post, we will see how to resolve How to convert object to Json in .net core 6 JsonConverter


Im trying to convert a object that looks like the object below into Json in .net core 6?
Before upgrading to .net core 6 the object would automatically get converted to json and sent to the backend in the proper format. Without any converters it gets sent back as an empty [].
The route im currently trying is using JsonConverter in my Program.cs file but not sure if this route makes sense.
The source value looks like below, but I convert it to the deserialized version above with this code. When I try to serialize it back is were the problem happens.
Source looks like this
Json Serializer returns empty []
serializerSourceData looks like this
Any ideas how to get this desired format?

Best Answer:

you are mixing in one bottle Newtonsoft.Json and System.Text.Json serializers. JObject and JsonObject are completely different classes. You will have to select something one
Now System.Text.Json
if you want to convert JObject to JsonObject, you can serialize a JObject to a json string, and after this deserialize the json string to a JsonObject.

If you have better answer, please add a comment about this, thank you!