-
Notifications
You must be signed in to change notification settings - Fork 0
/
xml_dict.py
32 lines (28 loc) · 1.09 KB
/
xml_dict.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import xmltodict
from collections.abc import MutableMapping
from collections.abc import MutableMapping, Iterable
import sys
import json
file_name = sys.argv[1]
with open(file_name, 'r') as file:
xml_data = file.read()
xml_dict=xmltodict.parse(xml_data)
#print(xml_dict)
def flatten(dictionary, parent_key='', separator='_'):
items = []
for key, value in dictionary.items():
new_key = parent_key + separator + key if parent_key else key
if isinstance(value, MutableMapping):
items.extend(flatten(value, new_key, separator=separator).items())
elif isinstance(value, Iterable) and not isinstance(value, str):
# If the value is a list, flatten each item with index prefix
for i, item in enumerate(value):
items.extend(flatten({f"{new_key}_{i}-{k}": v for k, v in item.items()}, separator=separator).items())
else:
items.append((new_key, value))
return dict(items)
new_dict = flatten(xml_dict)
#print(new_dict)
output = json.dumps(new_dict)
json_output = json.loads(output)
print(json.dumps(new_dict))