In [41]:
sample_content_of_teams_json = """{
"teams": [
{ "name": "MegaTeam1", "spacer": true,
"children": [
{ "name": "Team-1"},
{ "name": "Team-2"},
{ "name": "Team 3",
"children": [
{ "name": "team 3"},
{ "name": "team 121"}]},
{ "name": "Team 4"}]},
{ "name": "Team 5"},
{ "name": "Other"}
]
}"""
The code below digests the config and produces unique key for each team.
In [43]:
import json
from hashlib import md5
class TeamConfig(object):
"""Flattens teams.json config into a digestable structure"""
def __init__(self, config_json_string):
self.prefixes = []
for root_object in json.loads(config_json_string)["teams"]:
self.recursive_fix_(root_object)
def recursive_fix_(self, a, prefix=""):
"""A helper function to navigate the nested teams"""
prefix = prefix + "." if len(prefix) > 0 else prefix
self.prefixes.append(dict(
name=a['name'],
key=md5(prefix + a["name"]).hexdigest(),
spacer=True if "spacer" in a else False))
if "children" in a:
prefix += a["name"]
for node in a["children"]:
self.recursive_fix_(node, prefix)
An example of a flattened config goes below:
In [42]:
team_conf = TeamConfig(json_input)
team_conf.prefixes
Out[42]: