You should really try to do it yourself, show what you've done and where it doesn't work. But I had a few minutes free, so...
To parse the table, you can use the csv
module. I'll leave that to you.
Probably not an optimal solution, but this will do it:
datain = (
(1,'Root',1,16,-1),
(2,'Articles',2,11,1),
(5,'Fiction',3,8,2),
(7,'Fantasy',4,5,5),
(8,'Sci-fi',6,7,5),
(6,'Reference',9,10,2),
(3,'Portfolio',12,13,1),
(4,'Contact',14,15,1),
)
def convert_to_json(data):
node_index = dict()
parent_index = dict()
for node in data:
node_index[node[0]] = node
parent_index.setdefault(node[4],[]).append(node)
def process_node(index):
result = { 'data' : node_index[index][1] }
for node in parent_index.get(index,[]):
result.setdefault('children',[]).append(process_node(node[0]))
return result
node = process_node(1)
return [node]
Returns:
[
{
'data': 'Root',
'children': [
{
'data': 'Articles',
'children': [
{
'data': 'Fiction',
'children': [
{ 'data': 'Fantasy' },
{ 'data': 'Sci-fi' }
]
},
{ 'data': 'Reference' }
]
},
{ 'data': 'Portfolio' },
{ 'data': 'Contact' }
]
}
]