使用Python以点表示法打印唯一的JSON键

时间:2015-11-03 00:00:30

标签: python json python-2.7

我正在尝试编写一个脚本,用点符号打印JSON文件的唯一键,以便快速分析结构。

例如,让我说我有' myfile.json'使用以下格式:

{
"a": "one",
"b": "two",
"c": {
    "d": "four",
    "e": "five",
    "f": [
        {
            "x": "six",
            "y": "seven"
        },
        {
            "x": "eight",
            "y": "nine"
        }
    ]
}

运行以下内容将生成一组唯一的键,但它缺少谱系。

import json
json_data = open("myfile.json")
jdata = json.load(json_data)

def get_keys(dl, keys_list):
    if isinstance(dl, dict):
        keys_list += dl.keys()
        map(lambda x: get_keys(x, keys_list), dl.values())
    elif isinstance(dl, list):
        map(lambda x: get_keys(x, keys_list), dl)

keys = []
get_keys(jdata, keys)

all_keys = list(set(keys))

print '\n'.join([str(x) for x in sorted(all_keys)])

以下输出并未表明' x',' y'嵌套在' f'阵列。

a
b
c
d
e
f
x
y

我无法弄清楚如何遍历嵌套结构以附加父键。

理想的输出是:

a
b
c.d
c.e
c.f.x
c.f.y

2 个答案:

答案 0 :(得分:0)

我建议使用递归生成器函数,使用 yield 语句,而不是在内部构建列表。在Python 2.6+中,以下工作:

import json
json_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 path


for s in sorted(walk_keys(json_data)):
    print s

在Python 3.x中,您可以使用 yield from 作为递归生成的语法糖,如下所示:

import json
json_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 path


for s in sorted(walk_keys(json_data)):
    print(s)

答案 1 :(得分:0)

根据MTADD的指导,我将以下内容放在一起:

import json

json_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 path

all_keys = list(set(walk_keys(json_data)))

print '\n'.join([str(x) for x in sorted(all_keys)])

结果符合预期

a
b
c.d
c.e
c.f.x
c.f.y