[Fork] questions/50419383/how-to-convert-dynamic-json

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 Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;	

// https://stackoverflow.com/questions/50419383/how-to-convert-dynamic-json

    class TestClass
    {
        public static void Test()
        {
			var inputJson = GetJson();
			
			// Deserialize the input JSON as an array of arrays of JSON objects
			var inputArray = JsonConvert.DeserializeObject<JObject [][]>(inputJson);
			
			// Convert each inner array to a JSON object whose property names come from the nested objects' Key values and property values come from the nested objects' Value values:
			var outputArray = inputArray.Select(a => new JObject(a.Select(o => (new JProperty((string)o["Key"], o["Value"])))));
			
			// And re-serialize to JSON.
			var outputJson = JsonConvert.SerializeObject(outputArray, Formatting.Indented);
			
			Console.WriteLine(outputJson);
			
			Assert.IsTrue(JToken.DeepEquals(JToken.Parse(outputJson), JToken.Parse(GetRequiredJson())), "Generated and required JSON are NOT equivalent!");
			Console.WriteLine("Generated and required JSON are equivalent.");
		}
		
		static string GetJson()
		{
			var json = @"[
[{""Key"":""entity_id"",""Value"":""1""},{""Key"":""CustomerName"",""Value"":""Test1""},{""Key"":""AccountNumber"",""Value"":""ACC17-001""},{""Key"":""CustomerType"",""Value"":""Direct Sale""}],
[{""Key"":""entity_id"",""Value"":""2""},{""Key"":""CustomerName"",""Value"":""Test2""},{""Key"":""AccountNumber"",""Value"":""ACC17-002""},{""Key"":""CustomerType"",""Value"":""Direct Sale""}],
[{""Key"":""entity_id"",""Value"":""3""},{""Key"":""CustomerName"",""Value"":""Test3""},{""Key"":""AccountNumber"",""Value"":""ACC17-003""},{""Key"":""CustomerType"",""Value"":""Direct Sale""}],
[{""Key"":""entity_id"",""Value"":""4""},{""Key"":""CustomerName"",""Value"":""Test4""},{""Key"":""AccountNumber"",""Value"":""ACC17-004""},{""Key"":""CustomerType"",""Value"":""Direct Sale""}],
[{""Key"":""entity_id"",""Value"":""5""},{""Key"":""CustomerName"",""Value"":""Test5""},{""Key"":""AccountNumber"",""Value"":""ACC17-005""},{""Key"":""CustomerType"",""Value"":""Invoice""}],
[{""Key"":""entity_id"",""Value"":""6""},{""Key"":""CustomerName"",""Value"":""Test6""},{""Key"":""AccountNumber"",""Value"":""ACC17-006""},{""Key"":""CustomerType"",""Value"":""Invoice""}]
]";
			return json;
		}
		
		static string GetRequiredJson()
		{
			var json = @"[
{""entity_id"":""1"",""CustomerName"":""Test1"",""AccountNumber"":""ACC17-001"",""CustomerType"":""Direct Sale""},
{""entity_id"":""2"",""CustomerName"":""Test2"",""AccountNumber"":""ACC17-002"",""CustomerType"":""Direct Sale""},
{""entity_id"":""3"",""CustomerName"":""Test3"",""AccountNumber"":""ACC17-003"",""CustomerType"":""Direct Sale""},
{""entity_id"":""4"",""CustomerName"":""Test4"",""AccountNumber"":""ACC17-004"",""CustomerType"":""Direct Sale""},
{""entity_id"":""5"",""CustomerName"":""Test5"",""AccountNumber"":""ACC17-005"",""CustomerType"":""Invoice""},
{""entity_id"":""6"",""CustomerName"":""Test6"",""AccountNumber"":""ACC17-006"",""CustomerType"":""Invoice""}
]";
			return json;
		}
	}


	public class Program
	{
		public static void Main()
		{
			Console.WriteLine("Environment version: " + Environment.Version);
			Console.WriteLine("Json.NET version: " + typeof(JsonSerializer).Assembly.FullName);
			Console.WriteLine();

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

    public class AssertionFailedException : System.Exception
    {
        public AssertionFailedException() : base() { }

        public AssertionFailedException(string s) : base(s) { }
    }

    public static class Assert
    {
        public static void IsTrue(bool value)
        {
            IsTrue(value, "failed");
        }

        public static void IsTrue(bool value, string message)
        {
            if (value == false)
                throw new AssertionFailedException(message ?? "failed");
        }
    }
;