Last active
April 16, 2020 19:17
-
-
Save yoshikakbudto/c3271488c8fe2c6ecb429f5780f129d7 to your computer and use it in GitHub Desktop.
python convert json to xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def json2xml(json_obj): | |
"""Simple converter from json to xml | |
Kudos go to https://stackoverflow.com/questions/8988775/convert-json-to-xml-in-python. | |
""" | |
result_list = list() | |
json_obj_type = type(json_obj) | |
if json_obj_type is list: | |
for sub_elem in json_obj: | |
result_list.append(json2xml(sub_elem)) | |
return "".join(result_list) | |
if json_obj_type is dict: | |
for tag_name in json_obj: | |
sub_obj = json_obj[tag_name] | |
result_list.append("<{}>".format(tag_name)) | |
result_list.append(json2xml(sub_obj)) | |
result_list.append("</{}>".format(tag_name)) | |
return "".join(result_list) | |
return json_obj | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment