Skip to content

Instantly share code, notes, and snippets.

@yoshikakbudto
Last active April 16, 2020 19:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yoshikakbudto/c3271488c8fe2c6ecb429f5780f129d7 to your computer and use it in GitHub Desktop.
Save yoshikakbudto/c3271488c8fe2c6ecb429f5780f129d7 to your computer and use it in GitHub Desktop.
python convert json to xml
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