繁体   English   中英

使用 Python 将 JSON 嵌套到平面 JSON

[英]Nested JSON to Flat JSON using Python

我有一个嵌套的 JSON ,如下所示 -

样品4 = {“a”:1,“b”:2,“c”:3,“d”:[{“a”:5,“b”:6},{“a”:7,“b” : 8}], "e": [{"a": 1}, {"a": 2}], "f": 9, "g": [{"a": 5, "b": 6 },{“a”:7,“b”:8}],“i”:{“a”:5,“b”:6},“j”:{}}

我想把它转换成一个平面 JSON 文件。

目前,我正在使用此代码 -

def count_steps(dictionary):
    """counts the needed steps from the longest list inside the dictionary"""
    return max((len(value) for value in dictionary.values() if isinstance(value, list)))

def flatten(dictionary, name=''):
    steps = count_steps(dictionary)
    return_out = []
    for step in range(0, steps):
        out = {}
        for key, value in dictionary.items():
            if isinstance(value, list):
                for key_inner, value_inner in value[step].items():
                    combined_key = key + '_' + key_inner
                    if combined_key not in out:
                        out[combined_key] = []
                    out[combined_key] = value_inner
            else:
                out[key] = value
        return_out.append(out)
    return return_out

当我使用此代码时,我得到以下 output -

[{'a': 1,
  'b': 2,
  'c': 3,
  'd_a': 5,
  'd_b': 6,
  'e_a': 1,
  'f': 9,
  'g_a': 5,
  'g_b': 6,
  'i': {'a': 5, 'b': 6},
  'j': {}},
 {'a': 1,
  'b': 2,
  'c': 3,
  'd_a': 7,
  'd_b': 8,
  'e_a': 2,
  'f': 9,
  'g_a': 7,
  'g_b': 8,
  'h_a': 7,
  'h_b': 8,
  'i': {'a': 5, 'b': 6},
  'j': {}}]

但我想要以下 output -

[{'a': 1,
  'b': 2,
  'c': 3,
  'd_a': 5,
  'd_b': 6,
  'e_a': 1,
  'f': 9,
  'g_a': 5,
  'g_b': 6,
  'i_a': 5, 
  'i_b': 6,
  'j': {}},
 {'a': 1,
  'b': 2,
  'c': 3,
  'd_a': 7,
  'd_b': 8,
  'e_a': 2,
  'f': 9,
  'g_a': 7,
  'g_b': 8,
  'h_a': 7,
  'h_b': 8,
  'i_a': 5, 
  'i_b': 6,
  'j': {}}]

此处的代码首先计算 JSON 中存在的所有列表中的最大元素数。

我认为有更漂亮的方法来解决它,但我试图按照你的方式做最小的修改。

关键是关心类型(dict)。

sample4 = { 
    "a": 1, 
    "b": 2, 
    "c": 3, 
    "d": [{"a": 5, "b": 6}, {"a": 7, "b": 8}], 
    "e": [{"a": 1}, {"a": 2}], 
    "f": 9, 
    "g": [{"a": 5, "b": 6}, {"a": 7, "b": 8}], 
    "i": {"a": 5, "b": 6}, 
    "j": {} }


def count_steps(dictionary):
    """counts the needed steps from the longest list inside the dictionary"""
    return max((len(value) for value in dictionary.values() if isinstance(value, list)))

def merge_dict(outer_dict, inner_dict, key):
    for key_inner, value_inner in inner_dict.items():
        combined_key = key + '_' + key_inner
        outer_dict[combined_key] = value_inner

def flatten(dictionary, name=''):
    steps = count_steps(dictionary)
    return_out = []
    for step in range(0, steps):
        out = {}
        for key, value in dictionary.items():
            if isinstance(value, list):
                merge_dict(out, value[step], key)
                # for key_inner, value_inner in value[step].items():
                #     combined_key = key + '_' + key_inner
                #     if combined_key not in out:
                #         out[combined_key] = []
                #     out[combined_key] = value_inner
            elif isinstance(value, dict):
                #exception for "j"
                if len(value) == 0:
                    out[key] = {}
                else:
                    merge_dict(out, value, key)
            else:
                out[key] = value

        return_out.append(out)
    return return_out

sample5 = flatten(sample4)
print(sample5)
data = { "a": 1, "b": 2, "c": 3, "d": [{"a": 5, "b": 6}, {"a": 7, "b": 8}], "e": [{"a": 1}, {"a": 2}], "f": 9, "g": [{"a": 5, "b": 6}, {"a": 7, "b": 8}], "i": {"a": 5, "b": 6}, "j": {} } def flatten(dictionary): """counts the needed steps from the longest list inside the dictionary""" bag = [] # keys to be deleted new_dict = dict() # new keys to be added for key, value in dictionary.items(): if type(value) is list: bag.append(key) for _value in value: if type(_value) is dict: for key1, value2 in _value.items(): new_key = key + '_' + key1 new_dict[new_key] = value2 print((new_key, value2)) else: print((key, value)) for key in bag: del dictionary[key] for key, value in new_dict.items(): dictionary[key] = value return dictionary print(flatten(data))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM