sql org chart hierarchical table to xml json I have a table of employees and managers:

create table emp_mgr (emp varchar(99), mgr varchar(99))

insert into emp_mgr select 'Adam',NULL; insert into emp_mgr select 'Bob','Adam'; insert into emp_mgr select 'Charles','Bob'; ; insert into emp_mgr select 'David','Bob'; insert into emp_mgr select 'Ethan','Bob'; insert into emp_mgr select 'Frank','Charles'; insert into emp_mgr select 'George','Charles'; insert into emp_mgr select 'Henry','Charles'; insert into emp_mgr select 'Ian','David'; insert into emp_mgr select 'James','Ethan'; insert into emp_mgr select 'Ken','Ethan'; insert into emp_mgr select 'Larry','Ethan'; insert into emp_mgr select 'Mike','Frank'; insert into emp_mgr select 'Ned','Frank'; insert into emp_mgr select 'Oliver','Frank'; insert into emp_mgr select 'Peter','Frank'; insert into emp_mgr select 'Quinn','George'; insert into emp_mgr select 'Robert','George'; insert into emp_mgr select 'Scott','George'; insert into emp_mgr select 'Ted','George'; insert into emp_mgr select 'Urban','George'; insert into emp_mgr select 'Victor','Henry'; insert into emp_mgr select 'Wes','Ian'; insert into emp_mgr select 'Xavier','James'; insert into emp_mgr select 'Young','James'; insert into emp_mgr select 'Zach','James'; 


and I want to generate this json file from it :

{ name:"Adam" , children:[   { name:"Bob" , children:[     { name:"Charles" , children:[       { name:"Frank" , children:[         { name:"Mike" },         { name:"Ned" },         { name:"Oliver" },         { name:"Peter" }       ]},       { name:"George" , children:[         { name:"Quinn" },         { name:"Robert" },         { name:"Scott" },         { name:"Ted" },         { name:"Urban" }       ]},       { name:"Henry" , children:[         { name:"Victor" }       ]}     ]},     { name:"David" , children:[       { name:"Ian" , children:[         { name:"Wes" }       ]}     ]},     { name:"Ethan" , children:[       { name:"James" , children:[         { name:"Xavier" },         { name:"Young" },         { name:"Zach" }       ]},       { name:"Ken" },       { name:"Larry" }     ]}   ]} ]}


Here's what I tried - With a recursive xml query..

CREATE function [dbo].[SelectChild](@name as varchar(99))
returns xml
begin
    return (
        select 
            employee as "@name",
            dbo.SelectChild(employee)
        from emp_mgr
        where manager = @name
        for xml path('record'), elements
    )
end

select 
    employee as "@name", 
    dbo.SelectChild(employee)
from emp_mgr
where manager is null
for xml path ('record')


..I was able to generate this xml :

&lt;record name="Adam"&gt;   &lt;record name="Bob"&gt;     &lt;record name="Charles"&gt;       &lt;record name="Frank"&gt;         &lt;record name="Mike" /&gt;         &lt;record name="Ned" /&gt;         &lt;record name="Oliver" /&gt;         &lt;record name="Peter" /&gt;       &lt;/record&gt;       &lt;record name="George"&gt;         &lt;record name="Quinn" /&gt;         &lt;record name="Robert" /&gt;         &lt;record name="Scott" /&gt;         &lt;record name="Ted" /&gt;         &lt;record name="Urban" /&gt;       &lt;/record&gt;       &lt;record name="Henry"&gt;         &lt;record name="Victor" /&gt;       &lt;/record&gt;     &lt;/record&gt;     &lt;record name="David"&gt;       &lt;record name="Ian"&gt;         &lt;record name="Wes" /&gt;       &lt;/record&gt;     &lt;/record&gt;     &lt;record name="Ethan"&gt;       &lt;record name="James"&gt;         &lt;record name="Xavier" /&gt;         &lt;record name="Young" /&gt;         &lt;record name="Zach" /&gt;       &lt;/record&gt;       &lt;record name="Ken" /&gt;       &lt;record name="Larry" /&gt;     &lt;/record&gt;   &lt;/record&gt; &lt;/record&gt;


but don't know how to get the 'children' tag in there, and then how to convert it to json format ?

How do i generate a json file from hierarchical organization data using SQL ?
You can use the for xml path('') trick to concatenate the strings you need directly in the recursive function.

Something like this should do it for you:

create function dbo.SelectChild(@name as varchar(99)) returns varchar(max)
begin
return 
(
  select ',"children":[' + 
           stuff((
                 select ',{"name":"' + 
                          E.employee + '"'+
                          isnull(dbo.SelectChild(E.employee), '')+'}'
                 from dbo.emp_mgr as E
                 where E.manager = @name
                 for xml path(''), type
                 ).value('text()[1]', 'varchar(max)'), 1, 1, '')+']'
)
end

go

select '{"name":"' + E.employee + '"'+isnull(dbo.SelectChild(E.employee), '')+'}'
from dbo.emp_mgr as E
where E.manager is null


Result:

{  
   "name":"Adam",
   "children":[  
      {  
         "name":"Bob",
         "children":[  
            {  
               "name":"Charles",
               "children":[  
                  {  
                     "name":"Frank",
                     "children":[  
                        {  
                           "name":"Mike"
                        },
                        {  
                           "name":"Ned"
                        },
                        {  
                           "name":"Oliver"
                        },
                        {  
                           "name":"Peter"
                        }
                     ]
                  },
                  {  
                     "name":"George",
                     "children":[  
                        {  
                           "name":"Quinn"
                        },
                        {  
                           "name":"Robert"
                        },
                        {  
                           "name":"Scott"
                        },
                        {  
                           "name":"Ted"
                        },
                        {  
                           "name":"Urban"
                        }
                     ]
                  },
                  {  
                     "name":"Henry",
                     "children":[  
                        {  
                           "name":"Victor"
                        }
                     ]
                  }
               ]
            },
            {  
               "name":"David",
               "children":[  
                  {  
                     "name":"Ian",
                     "children":[  
                        {  
                           "name":"Wes"
                        }
                     ]
                  }
               ]
            },
            {  
               "name":"Ethan",
               "children":[  
                  {  
                     "name":"James",
                     "children":[  
                        {  
                           "name":"Xavier"
                        },
                        {  
                           "name":"Young"
                        },
                        {  
                           "name":"Zach"
                        }
                     ]
                  },
                  {  
                     "name":"Ken"
                  },
                  {  
                     "name":"Larry"
                  }
               ]
            }
         ]
      }
   ]
}