使用Python将JSON嵌套到平面JSON
我有一个嵌套的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文件 目前,我正在使用此代码-使用Python将JSON嵌套到平面JSON,python,json,python-3.x,Python,Json,Python 3.x,我有一个嵌套的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
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
当我使用这段代码时,我得到以下输出-
[{'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': {}}]
但我想要以下输出-
[{'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)
我原以为有更好的方法解决这个问题,但我试着按照你的方法去做 关键是关心类型(dict)
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},{
def展平(字典):
“”“从字典中最长的列表中计算所需的步骤”“”
bag=[]#要删除的密钥
new_dict=dict()#要添加的新键
对于键,dictionary.items()中的值:
如果类型(值)为列表:
包。附加(键)
对于_valuein value:
如果类型(_值)为dict:
对于_value.items()中的键1、值2:
new_key=key+''+key1
new_dict[new_key]=值2
打印((新_键,值2))
其他:
打印((键、值))
对于钥匙袋:
德尔字典[键]
对于键,新目录项()中的值:
字典[键]=值
返回字典
打印(展平(数据))
数据={“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展平(字典):
“”“从字典中最长的列表中计算所需的步骤”“”
bag=[]#要删除的密钥
new_dict=dict()#要添加的新键
对于键,dictionary.items()中的值:
如果类型(值)为列表:
包。附加(键)
对于_valuein value:
如果类型(_值)为dict:
对于_value.items()中的键1、值2:
new_key=key+''+key1
new_dict[new_key]=值2
打印((新_键,值2))
其他:
打印((键、值))
对于钥匙袋:
德尔字典[键]
对于键,新目录项()中的值:
字典[键]=值
返回字典
打印(展平(数据))
您有一个字典,而不是JSON。flatte(示例4)
不起作用。你能在dict
而不是图片中发布工作代码和所需输出吗?我已经编辑了这个问题。请查收。谢谢。你有字典,不是JSON。flatte(示例4)
不起作用。你能在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)