'How to convert xml to Json from one directory to other directory
I have a scenario like in one folder having 100 xmls so need to convert in to json with the same names of json instead of xml i have tried many ways but all xmls are converting into one json but i need separate json files
by using this all 100 xmls files are converting into one json file but i need 100 seprate json files, can anyone please suggest best approach
for path, dirs, files in os.walk(r"C:\exml\FND_GLS".format(type) ):
for f in files:
clinical = os.path.join(path, f)
print(clinical)
tree = ET.parse(clinical)
root = tree.getroot()
xmlstr = ET.tostring(root, encoding='utf-8', method='xml')
data_dict = dict(xmltodict.parse(xmlstr))
def write_json(target_path, target_file, data):
if not os.path.exists(target_path):
try:
os.makedirs(target_path)
except Exception as e:
print(e)
raise
with open(os.path.join(target_path, target_file), 'w') as f:
data = json.dump(data_dict,f, sort_keys=False, indent=4)
write_json('C:\ejson\FND_GLS_Json', 'my_json.json', 'data')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|