Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert json to flat structure in C#

Tags:

I'm trying to write function in C# that will converts JSON to a key/value pairs. It should support arrays. So for example the following JSON:

{ 
    title: title_value,
    components: [
        {
            component_id: id1,
            menu: [
                   {title: menu_title1},
                   {title: menu_title_x},
                   {id: menu_id1}    
            ]
        },
        {
             component_id: id2,
             menu: [
                   {title: menu_title2},
                   {id: menu_id2}    
             ]
        }
    ]
}

should be converted to:

  • title = title_value
  • components.0.component_id = id1
  • components.0.menu.0.title = menu_title1
  • components.0.menu.1.title = menu_title_x
  • components.0.menu.2.id = menu_id1
  • components.1.component_id = id2
  • components.1.menu.0.title = menu_title2
  • components.1.menu.1.id = menu_id2

Is it any simple way to do this task? The logic becomes complicated when I start taking into account arrays and nested arrays.