import os, json def path_convert(path): d = {} if os.path.isdir(path): d['text'] = os.path.basename(path) d['children'] = [ path_convert(os.path.join(path,x)) for x in sorted(os.listdir(path)) ] else: d['text'] = os.path.basename(path) return d print(json.dumps([ path_convert(y) for y in sorted(os.listdir('.'))] , indent=4))