Convert XML Data to JSON using Python

[436 views]


Converting XML file data to JSON

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

Program

import xmltodict import json with open('sample_xml_file.xml') as f: dict_val = xmltodict.parse(f.read()) 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": "http://www.smith-company-ltd.com", "logo": {"@alt": "Logo of Smith and Company Ltd.", "@address": "http://www.smith-company-ltd.com/logo.jpg"}}}
        

Algorithm Logic Test

Our Quiz prepared by Experts Helps you identify your knowledge in Algorithms. Everyone should atleast attempt this Quiz Once. 



Comments

2 comment
  • Prachi

    It is not working for me.

  • Shaddy

    You need to install the xmltodict module. Check this https://pypi.org/project/xmltodict/



Recommended Book Deals Ends in

Download 101 Ready to Use Excel Formulas ebook
Download Python All in One for Dummies ebook



Search
Online Games
Play 2048 Game Online and Relax.
Play 2048 Game Online

Search Tags