Parse an array of objects to a JSON tree structure

TheCoder 91 Reputation points
2023-01-27T12:38:51.11+00:00

I'm trying to use the bootstrap treeview and the JSON needs to be a certain format. I currently have this format:

var data =
[
    {
        make: "Chevy", model: "2500",  location: "lot"
    },
    {
        make: "Ford", model: "Edge",  location: "lot"
    },
    {
        make: "Chevy", model: "1500",  location: "lot"
    },
    {
        make: "Chevy", model: "Econoline",  location: "back lot"
    },
    {
        make: "GMC", model: "Acadia",  location: "back lot"
    },
    {
        make: "GMC", model: "Terrain",  location: "in transit"
    },
    {
        make: "Chevy", model: "Cruze",  location: "in transit"
    },
    {
        make: "Ford", model: "F150",  location: "ordered"
    }
]

But the bootstrap treeview is looking for this format:


var data = [
  {
    text: "Parent 1",
    nodes: [
      {
        text: "Child 1",
        nodes: [
          {
            text: "Grandchild 1"
          },
          {
            text: "Grandchild 2"
          }
        ]
      },
      {
        text: "Child 2"
      }
    ]
  },
  {
    text: "Parent 2"
  },
  {
    text: "Parent 3"
  },
  {
    text: "Parent 4"
  },
  {
    text: "Parent 5"
  }
];

How can I change the LINQ or even JQUERY code o get this format?

I'm looking to get the output to appear as the following structure:

Make
    Location
        Model
Chevy
    Lot
        2500
        1500
    Back Lot
        Econoline
Ford
    Lot
        Edge

And so on.

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,264 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,097 questions
0 comments No comments
{count} votes

Accepted answer
  1. Reza Aghaei 4,936 Reputation points MVP
    2023-01-27T14:50:57.6433333+00:00

    This is what I tried with Linq GroupBy and worked as expected:

    using Newtonsoft.Json;
    using System;
    using System.Linq;
    namespace SampleConsoleApp
    {
        internal class Program
        {
            static void Main(string[] args)
            {
                var json = @"
                [
                    {make: ""Chevy"", model: ""2500"",  location: ""lot""},
                    {make: ""Ford"", model: ""Edge"",  location: ""lot""},
                    {make: ""Chevy"", model: ""1500"",  location: ""lot""},
                    {make: ""Chevy"", model: ""Econoline"",  location: ""back lot""},
                    {make: ""GMC"", model: ""Acadia"",  location: ""back lot""},
                    {make: ""GMC"", model: ""Terrain"",  location: ""in transit""},
                    {make: ""Chevy"", model: ""Cruze"",  location: ""in transit""},
                    {make: ""Ford"", model: ""F150"",  location: ""ordered""}
                ]";
                var structure = new[] { new { make = "", model = "", location = "" } };
                var data = JsonConvert.DeserializeAnonymousType(json, structure).ToArray();
                var tree = data.GroupBy(x => x.make)
                    .Select(g => new
                    {
                        text = g.Key,
                        nodes = g.GroupBy(xx => xx.location)
                                 .Select(gg => new
                                 {
                                     text = gg.Key,
                                     nodes = gg.Select(xxx => new { text = xxx.model })
                                 })
                                 .ToArray()
                    }).ToArray();
                Console.WriteLine(JsonConvert.SerializeObject(tree, Formatting.Indented));
                Console.ReadLine();
            }
        }
    }
    
    

    Which prints out data:

    [
      {
        "text": "Chevy",
        "nodes": [
          {
            "text": "lot",
            "nodes": [
              {
                "text": "2500"
              },
              {
                "text": "1500"
              }
            ]
          },
          {
            "text": "back lot",
            "nodes": [
              {
                "text": "Econoline"
              }
            ]
          },
          {
            "text": "in transit",
            "nodes": [
              {
                "text": "Cruze"
              }
            ]
          }
        ]
      },
      {
        "text": "Ford",
        "nodes": [
          {
            "text": "lot",
            "nodes": [
              {
                "text": "Edge"
              }
            ]
          },
          {
            "text": "ordered",
            "nodes": [
              {
                "text": "F150"
              }
            ]
          }
        ]
      },
      {
        "text": "GMC",
        "nodes": [
          {
            "text": "back lot",
            "nodes": [
              {
                "text": "Acadia"
              }
            ]
          },
          {
            "text": "in transit",
            "nodes": [
              {
                "text": "Terrain"
              }
            ]
          }
        ]
      }
    ]
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful