Print unique JSON keys in dot notation using Python Print unique JSON keys in dot notation using Python json json

Print unique JSON keys in dot notation using Python


I'd recommend using a recursive generator function, using the yield statement rather than building a list internally. In Python 2.6+, the following works:

import jsonjson_data = json.load(open("myfile.json"))def walk_keys(obj, path=""):    if isinstance(obj, dict):        for k, v in obj.iteritems():            for r in walk_keys(v, path + "." + k if path else k):                yield r    elif isinstance(obj, list):        for i, v in enumerate(obj):            s = "[" + str(i) + "]"            for r in walk_keys(v, path + s if path else s):                yield r    else:        yield pathfor s in sorted(walk_keys(json_data)):    print s

In Python 3.x, you can use yield from as syntactic sugar for recursive generation, as follows:

import jsonjson_data = json.load(open("myfile.json"))def walk_keys(obj, path=""):    if isinstance(obj, dict):        for k, v in obj.items():            yield from walk_keys(v, path + "." + k if path else k)    elif isinstance(obj, list):        for i, v in enumerate(obj):            s = "[" + str(i) + "]"            yield from walk_keys(v, path + s if path else s)    else:        yield pathfor s in sorted(walk_keys(json_data)):    print(s)


Drawing off of MTADD's guidance I put together the following:

import jsonjson_file_path = "myfile.json"json_data = json.load(open(json_file_path))def walk_keys(obj, path = ""):    if isinstance(obj, dict):        for k, v in obj.iteritems():            for r in walk_keys(v, path + "." + k if path else k):                yield r    elif isinstance(obj, list):        for i, v in enumerate(obj):            s = ""            for r in walk_keys(v, path if path else s):                yield r    else:        yield pathall_keys = list(set(walk_keys(json_data)))print '\n'.join([str(x) for x in sorted(all_keys)])

The results match as expected

abc.dc.ec.f.xc.f.y