I want to parse this input to output.
<code>input = {
"a.b.c[0][0].d": "i",
"a.b.c[0][1].e-f": "j",
"a.b.c[0][2].g-h[0]": "x",
"a.b.c[0][3].g-h[1]": "y",
"a.b.c[0][4].g-h[2]": "z",
"x": [
{
"b": {
"x.y.z": "a"
},
},
{
"b": {
"d.f.a": "b"
},
},
{
"b": {
"g.h.q": "c"
},
},
],
}
output = {
"a": {
"b" : {
"c": [
[
{
"d": "i",
"e-f": "j",
"g-h": [ "x", "y", "z"]
}
]
]
}
},
"x": [
{
"b": {
"x": { "y": { "z": "a" } }
}
},
{
"b": {
"d": { "f": { "a": "b" } }
}
},
{
"b": {
"g": { "h": { "q": "c" } }
}
}
]
}
</code>
<code>input = {
"a.b.c[0][0].d": "i",
"a.b.c[0][1].e-f": "j",
"a.b.c[0][2].g-h[0]": "x",
"a.b.c[0][3].g-h[1]": "y",
"a.b.c[0][4].g-h[2]": "z",
"x": [
{
"b": {
"x.y.z": "a"
},
},
{
"b": {
"d.f.a": "b"
},
},
{
"b": {
"g.h.q": "c"
},
},
],
}
output = {
"a": {
"b" : {
"c": [
[
{
"d": "i",
"e-f": "j",
"g-h": [ "x", "y", "z"]
}
]
]
}
},
"x": [
{
"b": {
"x": { "y": { "z": "a" } }
}
},
{
"b": {
"d": { "f": { "a": "b" } }
}
},
{
"b": {
"g": { "h": { "q": "c" } }
}
}
]
}
</code>
input = {
"a.b.c[0][0].d": "i",
"a.b.c[0][1].e-f": "j",
"a.b.c[0][2].g-h[0]": "x",
"a.b.c[0][3].g-h[1]": "y",
"a.b.c[0][4].g-h[2]": "z",
"x": [
{
"b": {
"x.y.z": "a"
},
},
{
"b": {
"d.f.a": "b"
},
},
{
"b": {
"g.h.q": "c"
},
},
],
}
output = {
"a": {
"b" : {
"c": [
[
{
"d": "i",
"e-f": "j",
"g-h": [ "x", "y", "z"]
}
]
]
}
},
"x": [
{
"b": {
"x": { "y": { "z": "a" } }
}
},
{
"b": {
"d": { "f": { "a": "b" } }
}
},
{
"b": {
"g": { "h": { "q": "c" } }
}
}
]
}
I have done for simple keys however I am stuck at the list indices keys e.g. a[0][0] , which should create a list of values.
I am missing the parse for this type of keys. It should take the key and return a list depending how many level deep it needs to go based on the indices.
<code>def convert(self, data: Dict):
if isinstance(data, dict):
new_dict = {}
for key, value in data.items():
if not key or key is None or key == '':
continue
parts = key.split(".") if "." in key else key.split("_")
current_dict = new_dict
for part in parts[:-1]:
#print("Current Dict", current_dict)
current_dict.setdefault(part, {})
current_dict = current_dict[part]
if isinstance(value, dict):
current_dict[parts[-1]] = self.convert(value)
elif isinstance(value, list):
# Handle lists containing nested dictionaries
new_list = []
for item in value:
if isinstance(item, dict):
new_list.append(self.convert(item))
elif isinstance(item, str) and "," in item:
# Split comma-separated string and convert to list of strings
new_list.extend(item.split(","))
else:
new_list.append(item)
current_dict[parts[-1]] = new_list
else:
current_dict[parts[-1]] = value
return new_dict
else:
return data
print(convert(input))
</code>
<code>def convert(self, data: Dict):
if isinstance(data, dict):
new_dict = {}
for key, value in data.items():
if not key or key is None or key == '':
continue
parts = key.split(".") if "." in key else key.split("_")
current_dict = new_dict
for part in parts[:-1]:
#print("Current Dict", current_dict)
current_dict.setdefault(part, {})
current_dict = current_dict[part]
if isinstance(value, dict):
current_dict[parts[-1]] = self.convert(value)
elif isinstance(value, list):
# Handle lists containing nested dictionaries
new_list = []
for item in value:
if isinstance(item, dict):
new_list.append(self.convert(item))
elif isinstance(item, str) and "," in item:
# Split comma-separated string and convert to list of strings
new_list.extend(item.split(","))
else:
new_list.append(item)
current_dict[parts[-1]] = new_list
else:
current_dict[parts[-1]] = value
return new_dict
else:
return data
print(convert(input))
</code>
def convert(self, data: Dict):
if isinstance(data, dict):
new_dict = {}
for key, value in data.items():
if not key or key is None or key == '':
continue
parts = key.split(".") if "." in key else key.split("_")
current_dict = new_dict
for part in parts[:-1]:
#print("Current Dict", current_dict)
current_dict.setdefault(part, {})
current_dict = current_dict[part]
if isinstance(value, dict):
current_dict[parts[-1]] = self.convert(value)
elif isinstance(value, list):
# Handle lists containing nested dictionaries
new_list = []
for item in value:
if isinstance(item, dict):
new_list.append(self.convert(item))
elif isinstance(item, str) and "," in item:
# Split comma-separated string and convert to list of strings
new_list.extend(item.split(","))
else:
new_list.append(item)
current_dict[parts[-1]] = new_list
else:
current_dict[parts[-1]] = value
return new_dict
else:
return data
print(convert(input))
This results in:
<code>{'a': {'b': {'c[0][0]': {'d': 'i'}, 'c[0][1]': {'e-f': 'j'}, 'c[0][2]': {'g-h[0]': 'x'}, 'c[0][3]': {'g-h[1]': 'y'}, 'c[0][4]': {'g-h[2]': 'z'}}}, 'x': [{'b': {'x': {'y': {'z': 'a'}}}}, {'b': {'d': {'f': {'a': 'b'}}}}, {'b': {'g': {'h': {'q': 'c'}}}}]}
</code>
<code>{'a': {'b': {'c[0][0]': {'d': 'i'}, 'c[0][1]': {'e-f': 'j'}, 'c[0][2]': {'g-h[0]': 'x'}, 'c[0][3]': {'g-h[1]': 'y'}, 'c[0][4]': {'g-h[2]': 'z'}}}, 'x': [{'b': {'x': {'y': {'z': 'a'}}}}, {'b': {'d': {'f': {'a': 'b'}}}}, {'b': {'g': {'h': {'q': 'c'}}}}]}
</code>
{'a': {'b': {'c[0][0]': {'d': 'i'}, 'c[0][1]': {'e-f': 'j'}, 'c[0][2]': {'g-h[0]': 'x'}, 'c[0][3]': {'g-h[1]': 'y'}, 'c[0][4]': {'g-h[2]': 'z'}}}, 'x': [{'b': {'x': {'y': {'z': 'a'}}}}, {'b': {'d': {'f': {'a': 'b'}}}}, {'b': {'g': {'h': {'q': 'c'}}}}]}