html - Output an existing defaultdict into appropriate JSON format for flare dendogram Python -
i have defaultdict(list) , used simplejson.dums(my_defaultdict) in order output defaultdict json format. using html code dendogram http://bl.ocks.org/mbostock/4063570 trying make defaultdict information format of json file author using. json file named: /mbostock/raw/4063550/flare.json , it's found in link: http://bl.ocks.org/mbostock/raw/4063550/flare.json.
so here defaultdict data:
my_defaultdict = {5: ['child10'], 45: ['child92', 'child45'], 33:['child38']} json_data = simplejson.dumps(my_defaultdict) so current json_data looks this:
{ "5": [ "child10" ], "45": [ "child92", "child45" ], "33": [ "child38" ] } so in understanding numbers corresponding "name":"5" , json format file have children "children". right now, json format output doesn't run in html code of dendogram.
any on how output defaultdict appropriate json format work html code producing dendogram great. code has python.
the expected outcome this:
{ "name": "flare", "children": [ { "name": "5", "children": [ { "name": "child10", "size": 5000}, ] { "name": "45", "children": [ {"name": "child92", "size": 3501}, {"name": "child45", "size": 3567}, ] }, { "name": "33", "children": [ {"name": "child38", "size": 8044} ] } } edited: answer of martineau works, it's not want. start defaultdict(list) , desired output, above should have "children" list of dicts whereas martineau kind answer, "children" it's list. if can add make work great. don't worry "size" variable, can ignored now. thank you.
you need make new dictionary defaultdict. children in example code list of strings, don't know "size" of each 1 comes changed list of dicts (which don't have entry "size" key).
from collections import defaultdict #import simplejson json import json # using stdlib module instead my_defaultdict = defaultdict(list, { 5: ['child10'], 45: ['child92', 'child45'], 33: ['child38']}) my_dict = {'name': 'flare', 'children': [{'name': k, 'children': [{'name': child} child in v]} k, v in my_defaultdict.items()]} json_data = json.dumps(my_dict, indent=2) print(json_data) output:
{ "name": "flare", "children": [ { "name": 33, "children": [ { "name": "child38" } ] }, { "name": 5, "children": [ { "name": "child10" } ] }, { "name": 45, "children": [ { "name": "child92" }, { "name": "child45" } ] } ] }
Comments
Post a Comment