Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hierarchical JSON Data to Hierarchical Table

Tags:

c#

sql

json.net

I have a class like this and to this type I need to deserialize a JSON string

public class nodes
{
    public int id{get;set;}
    public string name{get;set;}
    public List<nodes> children{get;set;}
}

and the JSON is like this

{
  id: 15,
  name: 'user1',
  children: [
    {
      id: 22,
      name: 'user2',
      children: [
        {
          id: 34,
          name: 'user3',
          children: [
            {
              id: 43,
              name: 'user4',
              children: []
            },
            {
              id: 54,
              name: 'user5',
              children: []
            }
          ]
        },
        {
          id: 65,
          name: 'user6',
          children: []
        }
      ]
    },
    {
      id: 72,
      name: 'user7',
      children: []
    }
  ]
}

This is how I'm deserialising

node d=JsonConvert.DeserializeObject<node>(myJSON); //myJSON is my above JSON string

But the requirement is I need to insert these data into an SQL table as separate rows. The table should is as follows

UniqueID    id    name    ParentID
------------------------------------
    1       15    User1     0
    2       22    User2     1
    3       34    User3     2
    4       43    User4     3
    5       54    User5     3
    6       65    User6     2
    7       72    User7     1
--------------------------------------

As you see the table there is a system generated ID column UniqueID. Also another column ParentID to keep the hierarchy..

I can ofcourse use some recursive functions to handle each children to their details and create a dynamic query to insert. But I dont think its a best solution. Please suggest a better way to do this

like image 309
Sandeep Thomas Avatar asked Mar 27 '18 07:03

Sandeep Thomas


People also ask

Is JSON hierarchical data?

JSON is an ideal format for larger data that have a hierarchical structured relationship. The structure of a JSON object is as follows: The data are in name/value pairs. Data objects are separated by commas.

Can JSON and XML represent hierarchical data?

Self-describing: Both json and xml are self-describing as both xml data and json data are human-readable text. Hierarchical: Both json and xml support hierarchical structure.

What is hierarchical table?

A hierarchy table specifies a hierarchy of terms (such as drugs and events) available for selecting criteria for data mining results before viewing them. A hierarchy table has no effect on the generation of statistical results for a run.

What is hierarchical data type?

Hierarchical data is defined as a set of data items that are related to each other by hierarchical relationships. Hierarchical relationships exist where one item of data is the parent of another item.

How to deserialize the hierarchical data from JSON?

Thanks to the flat representation in the JSON file, the code for deserializing the hierarchical data ( read_from_json method) is very simple. What if you need to implement more complex search conditions on the taxonomy?

Can openjson parse JSON into a hierarchy table?

In this article OpenJSON is destined for greater and more complicated usage to deal with the cases where the JSON is hierarchical. I was asked the other day how to use OpenJSON to parse JSON into a hierarchy table like the one I used.

How to de-normalize JSON structures to relational format?

Data Flow: For advanced hierarchical to tabular transformation, we have to use Data Flow. This copy activity will de-normalize the JSON structures into relational format w.r.t array present in JSON. How much the denormalization will happened that depends on no of element in the array.

How to convert a hierarchical table to an XML file?

Converting the data from its Hierarchical table form will be different for each application, but is easy with a CTE. You can, alternatively, convert the hierarchical table into XML and interrogate that with XQuery


1 Answers

How about flattening your tree via Linq (it is still going to use recursion), I think it may make it clearer. You could then just loop throught the result. I'm thinking something like this:

First write an extension method:

public static IEnumerable<T> Flatten<T>(
    this IEnumerable<T> c,
    Func<T,IEnumerable<T>> f) 
{
    return c.SelectMany(n => f(n).Flatten(f)).Concat(c);
}

Then you can flatten and loop:

var nodesList = nodes.Flatten(node => node.children).ToList();

foreach(var n in nodeList)
{
    /add to db
}
like image 107
Kevin Avatar answered Oct 20 '22 23:10

Kevin