簡體   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