0

I have a list of data from the Mongo database. Content is in JSON format. It is an array of object and values have a parent relationship with each other.Object which have parent as null("") means it is a root node.

[  
   {  
      "_id":"5c04fc163838b0772dd9636d",
      "Company":"TESTCOMPANY",
      "id":"test_uk",
      "parent":""
   },
   {  
      "_id":"5c05181f0ab89a44a969015d",
      "Company":"TESTCOMPANY",
      "id":"Gateway",
      "parent":"test_uk"
   },
   {  
      "_id":"5c0518723838b0772dd9678e",
      "Company":"TESTCOMPANY",
      "id":"Device1",
      "parent":"Gateway"
   },
{  
      "_id":"5c0518723838b077789636e",
      "Company":"TESTCOMPANY",
      "id":"Device2",
      "parent":"Gateway"
   },
   {  
      "_id":"5c0518723838b0772dd9636e34",
      "Company":"TESTCOMPANY",
      "id":"Adapter",
      "parent":"test_uk"
   },
      {  
      "_id":"5c0518723838b0772dd9636e",
      "Company":"TESTCOMPANY",
      "id":"AdapterDevice",
      "parent":"Adapter"
   },

   {  
      "_id":"5c04fc163838b0772dd93454d",
      "Company":"TESTCOMPANY",
      "id":"test_us",
      "parent":""
   },
   {  
      "_id":"5c0518723838b0772dd9636e",
      "Company":"TESTCOMPANY",
      "id":"Device",
      "parent":"test_us"
   }
]

is it possible to convert as the parent-child format in c#?

expected format

[  
   {  
      "_id":"5c04fc163838b0772dd9636d",
      "Company":"TESTCOMPANY",
      "id":"test_uk",
      "children":[
             {  
                 "_id":"5c05181f0ab89a44a969015d",
                  "Company":"TESTCOMPANY",
                  "id":"Gateway",
                  "children":[
                      {  
                       "_id":"5c0518723838b0772dd9678e",
                       "Company":"TESTCOMPANY",
                       "id":"Device1"
                      },
                     {  
                      "_id":"5c0518723838b077789636e",
                      "Company":"TESTCOMPANY",
                      "id":"Device2"
                       }
                    ]
               },
        {  
           "_id":"5c0518723838b0772dd9636e34",
           "Company":"TESTCOMPANY",
           "id":"Adapter",
        "children":[  {  
             "_id":"5c0518723838b0772dd9636e",
             "Company":"TESTCOMPANY",
             "id":"AdapterDevice"
            }]
       }]},
     {  
      "_id":"5c04fc163838b0772dd93454d",
      "Company":"TESTCOMPANY",
      "id":"test_us",
  "children":[{
      "_id":"5c0518723838b0772dd9636e",
      "Company":"TESTCOMPANY",
      "id":"Device",
      "parent":"test_us"
     }]
    }
]

i just want to display this format tree strucutre in web Ui

jdweng
  • 33,250
  • 2
  • 15
  • 20
leo Martin
  • 180
  • 1
  • 3
  • 15
  • @nemanja228 this is not an answer.please explain at least – leo Martin Dec 03 '18 at 15:06
  • I read this article but it's not useful to my code https://stackoverflow.com/questions/3142495/deserialize-json-into-c-sharp-dynamic-object – leo Martin Dec 03 '18 at 15:11
  • The link for deserializing is the correct method to take you json data and put into classes. You then need to group the classes by Company/id and then serialize to get the format you are looking for. – jdweng Dec 03 '18 at 16:41

1 Answers1

0

I am not sure if it will be fast enough, but it could be a possible solution:

using System.Collections.Generic;
using Newtonsoft.Json;

namespace ConsoleApp1
{
    class Program
    {
        public class Item
        {
            [JsonProperty("_id")]
            public string Guid { get; set; }

            [JsonProperty(PropertyName = "company")]
            public string Company { get; set; }

            [JsonProperty(PropertyName = "id")]
            public string Id { get; set; }

            [JsonProperty(PropertyName = "parent")]
            public string Parent { get; set; }

            [JsonProperty(PropertyName = "children")]
            public List<Item> Children { get; set; }
        }


        private static string jsonString = "[{\"_id\":\"5c04fc163838b0772dd9636d\",\"Company\":\"TESTCOMPANY\",\"id\":\"test_uk\",\"parent\":\"\"},{\"_id\":\"5c05181f0ab89a44a969015d\",\"Company\":\"TESTCOMPANY\",\"id\":\"Gateway\",\"parent\":\"test_uk\"},{\"_id\":\"5c0518723838b0772dd9678e\",\"Company\":\"TESTCOMPANY\",\"id\":\"Device1\",\"parent\":\"Gateway\"},{\"_id\":\"5c0518723838b077789636e\",\"Company\":\"TESTCOMPANY\",\"id\":\"Device2\",\"parent\":\"Gateway\"},{\"_id\":\"5c0518723838b0772dd9636e34\",\"Company\":\"TESTCOMPANY\",\"id\":\"Adapter\",\"parent\":\"test_uk\"},{\"_id\":\"5c0518723838b0772dd9636e\",\"Company\":\"TESTCOMPANY\",\"id\":\"AdapterDevice\",\"parent\":\"Adapter\"},{\"_id\":\"5c04fc163838b0772dd93454d\",\"Company\":\"TESTCOMPANY\",\"id\":\"test_us\",\"parent\":\"\"},{\"_id\":\"5c0518723838b0772dd9636e\",\"Company\":\"TESTCOMPANY\",\"id\":\"Device\",\"parent\":\"test_us\"}]";

        static void Main(string[] args)
        {
            var items =  JsonConvert.DeserializeObject<List<Item>>(jsonString);

            var dictionary = new Dictionary<string, Item>();

            foreach (var item in items)
            {
                if (!dictionary.ContainsKey(item.Parent))
                {   
                    dictionary.Add(item.Id, item);
                }
                else
                {
                    if (dictionary[item.Parent].Children == null)
                        dictionary[item.Parent].Children = new List<Item>();
                    dictionary[item.Parent].Children.Add(item);
                }
            }

            string json = JsonConvert.SerializeObject(dictionary, Formatting.Indented);

            System.Console.WriteLine(json);

            System.Console.ReadLine();
        }
    }
}

I just made a simple console app which uses Newtonsoft.Json library. I think it is the mostly used for such a purposes.

You have to find on your own how to remove parent properties in the result, but it should not be a big deal, I suppose.

Belurd
  • 772
  • 1
  • 13
  • 31