https://stackoverflow.com/questions/72640451/create-json-object-using-system-text-json#72640451

CSharp · Console

/Program.cs

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization.Formatters;
using System.IO;
using System.Data;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Reflection;
using System.Collections.ObjectModel;
using System.Dynamic;

using System.Text.Json;
using System.Text.Json.Serialization;

using Newtonsoft.Json.Linq;

using NUnit.Framework; // Throws NUnit.Framework.AssertionException

//https://stackoverflow.com/questions/72640451/create-json-object-using-system-text-json#72640451

    class TestClass
    {
        public static void Test()
        {
			var result = new 
			{
				Status = "ok",
				TotalDuration = new 
				{
					TotalSeconds = 101.2M,
				},
				Entries = new []
				{
					new
					{
						Key = "some key",
						Value = new
						{
							Status = "ok",
							Duration = new
							{
								TotalSeconds = 101.2M,
							},
							Description = "my description",
						},
					},
				},
			};
			
			string json1, json2;
			
			Console.WriteLine("Result of serializing with System.Text.Json:");
			{
				var json = new Dictionary<string, object>
				{
					["Status"] = result.Status.ToString(),
					["Duration"] = result.TotalDuration.TotalSeconds.ToString(NumberFormatInfo.InvariantInfo),
				};

				var entries = result.Entries.ToDictionary(
					d => d.Key,
					d => new { Status = d.Value.Status.ToString(), Duration = d.Value.Duration.TotalSeconds.ToString(NumberFormatInfo.InvariantInfo), Description = d.Value.Description } );
				if (entries.Count > 0)
					json.Add("result", entries);
				
				var newJson = JsonSerializer.Serialize(json, new JsonSerializerOptions { WriteIndented = true });
				
				Console.WriteLine(newJson);
				
				json1 = newJson;
			}
			
			Console.WriteLine("Result of serializing with Json.NET:");
			{
				var json = new JObject(
					new JProperty("Status", result.Status.ToString()),
					new JProperty("Duration", result.TotalDuration.TotalSeconds.ToString()));
				if (result.Entries.Any())
				{
					var entries = new JObject(result.Entries
											  .Select(d => new JProperty(d.Key, new JObject(new JProperty("Status", d.Value.Status.ToString()), new JProperty("Duration", d.Value.Duration.TotalSeconds.ToString()), new JProperty("Description", d.Value.Description)))));

					json.Add(new JProperty("result", entries));
				}
				
				var newJson = json.ToString();
				
				Console.WriteLine(newJson);
				
				json2 = newJson;
			}
			
			Assert.That(JToken.DeepEquals(JToken.Parse(json1), JToken.Parse(json2)));
		}	
	}

	public class Program
	{
		public static void Main()
		{
			Console.WriteLine("Environment version: {0} ({1}), {2}", System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription , Environment.Version, Environment.OSVersion);
			Console.WriteLine("System.Text.Json version: " + typeof(JsonSerializer).Assembly.FullName);
			Console.WriteLine();

			try
			{
				TestClass.Test();
			}
			catch (Exception ex)
			{
				Console.WriteLine("Failed with unhandled exception: ");
				Console.WriteLine(ex);
				throw;
			}
		}
	}
;