Search code examples
pythonjsontreeidentifier

Building a json tree structure from identifiers


I have a file with data that looks like that :

ID attribute
1 'text'
101 'text'
1011 'text'
10111 'text'
1011101 'text'
1011102 'text'
1011103 'text'
1011104 'text'
1011130 'text'

My goal is to build json tree structure from these data :

{
    [
    ID : 1,
    attribute : 'text',
    children : [
        ID: 101,
        attribute : 'text',
        children : [
             ...
    ID : 2,
        ...
    ]
}

In python, i builded a list of dictionnaries like that :

[ {'id': ID, 'attr' : text}, {...} ]

I think i could use the fact that leaf id contain his parents id but i can't see the way to build the structure i want.

I would appreciate any help, in pseudo code or any other programming langage.


Solution

  • Solution from thg435 worked with little changes :

    # open & read raw file
    f=open(args[0], 'r')
    text = f.read()
    
    #
    text = map(lambda s: s.split(" ", 1), text.strip().replace("'","").splitlines())
    tree = [{'prefix': '', 'children':[]}]
    stack = [tree[0]]
    
    for id, attr in text:
        while not id.startswith(stack[-1]['prefix']):
            stack.pop()
        node = {'prefix': id, 'attr': attr, 'children': []}
        stack[-1]['children'].append(node)
        stack.append(node)
    
    pprint.pprint( tree)
    
    print json.dumps( tree)
    f=open(args[1], 'w')
    f.write(json.dumps( tree, sort_keys=True, indent=1))
    

    Thanks !