I have the following code to create a JSON file based on data seperated by ;
.
import csv
from collections import defaultdict
def ctree():
""" One of the python gems. Making possible to have dynamic tree structure.
"""
return defaultdict(ctree)
def build_leaf(name, leaf):
""" Recursive function to build desired custom tree structure
"""
res = {"name": name}
# add children node if the leaf actually has any children
if len(leaf.keys()) > 0:
res["children"] = [build_leaf(k, v) for k, v in leaf.items()]
return res
def main(delimter):
tree = ctree()
text = """
something;cookie;chocolat
something;cookie;milk
something;gains;bread
anything;protein;chicken
"""
rows = [row.strip().split(delimter) for row in text.split("\n")]
for row in rows:
if row:
leaf = tree[row[0]]
for cid in range(1, len(row)):
leaf = leaf[row[cid]]
# building a custom tree structure
res = []
for name, leaf in tree.items():
res.append(build_leaf(name, leaf))
# printing results into the terminal
import json
print(json.dumps(res))
# so let's roll
main(";")
(source: Convert csv to JSON tree structure? )
This will produce this output:
[
{
"name": ""
},
{
"name": "something",
"children": [
{
"name": "cookie",
"children": [
{
"name": "chocolat"
},
{
"name": "milk"
}
]
},
{
"name": "gains",
"children": [
{
"name": "bread"
}
]
}
]
},
{
"name": "anything",
"children": [
{
"name": "protein",
"children": [
{
"name": "chicken"
}
]
}
]
}
]
However I want to inlcude a , "size" : 100
, so I have an output like this:
[
{
"name": ""
},
{
"name": "something",
"children": [
{
"name": "cookie",
"children": [
{
"name": "chocolat", "size" : 100
},
{
"name": "milk", "size" : 100
}
]
},
{
etc.
So actually it needs to add , "size" : 100
if an item is added to the 3rd
layer. I'm wondering if this is still possible using recursion as above? or should I change it to nested loops (which I not prefer) to be able to "remember" the current layer? If not how can I achieve this while still using recursion