The query is mostly related to Jinja2 to recurse through a loop and add the elements into a recursive JSON type format.
For example, got a pipe separated list/data which is a "pipe separated hierarchy" as below
World|North_America|USA|Texas
World|Europe|UK|England|London
World|Australia
The hierarchy can be of any level and I wanted to have a JSON format based on the hierarchy by using Jinja2 template
What I've tried is
{% for item in pipe_separated_hierarchy.split('|') %}
"obj_{{loop.index}}": {
"{{item}}": {
"my_description_obj{{loop.index}}",
"obj_{{loop.index}}": {
<<<loop from 2nd iteration again >>> # not sure how to loop.index & item ?
}
{% endfor %}
I'm looking for an output
"obj_1": {
"World": {
"obj_2": {
"North_America": {
"obj_3": {
"USA": {
"obj_4": "Texas"
}
}
}
}
}
}
Any idea how to loop through the list while maintaining the count alongside and indent?