Convert XML Data to JSON using Python


Converting XML file data to JSON

  1. Import xmltodict
  2. Open the file data and convert it into dictionary
  3. Now convert the dictionary to Json using json.dumps


import xmltodict import json with open('sample_xml_file.xml') as f: dict_val = xmltodict.parse( json_data = json.dumps(dict_val) with open('xml_json.json','w') as fp: fp.write(json_data)

{ "letter": { "title": [ { "@maxlength": "10", "#text": "Quote Letter" }, { "@maxlength": "40", "#text": "Quote Details" } ], "salutation": { "@limit": "40", "#text": "Dear Daniel," }, "text": [ { "emphasis": "SDL Trados Studio 2015", "component": [ { "@translate": "yes", "#text": "XML Parser rules" }, { "@translate": "no", "#text": "XML" } ], "#text": "Thank you for sending us the information on\n. We like\nyour products and think they certainly represent the most powerful\ntranslation solution on the market. We especially like the\n\noptions in the filter. It has helped us to\nset up support for our XML files in a flash. We have already\ndownloaded the latest version from your Customer Center." }, "We would like to order 50 licenses.\nPlease send us a quote. Keep up the good work!" ], "greetings": { "@minlength": "10", "#text": "Yours sincerely," }, "signature": "Paul Smith", "address": [ { "@translate": "yes", "#text": "Smith & Company Ltd." }, { "@translate": "no", "#text": "Smithtown" } ], "weblink": "", "logo": { "@alt": "Logo of Smith and Company Ltd.", "@address": "" } } }



2 comment
  • Prachi

    It is not working for me.

  • Shaddy

    You need to install the xmltodict module. Check this


Need Freelance Programming Writers, Contact us now at

Having Programming Doubts or Facing any Issues?

Hurry Up! Exclusive Deals For You ends in

Quiz For You:
Online Games
Play 2048 Game Online and Relax.
Play 2048 Game Online

Search Tags