Reputation: 89
I have a Nested JSON as shown below -
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": {} }
I want to convert this into a flat JSON file.
Currently, I am using this code -
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
When I am using this code, I get the following 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': {}}]
But I want the following 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': {}}]
The code here first counts the maximum number of elements in all the lists present in the JSON.
Upvotes: 0
Views: 208
Reputation: 1896
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))
Upvotes: 1
Reputation: 667
I thought there is more beautiful way to solve it, but I tried to follow your way and do smallest modifying.
The key is to care about type(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)
Upvotes: 1