Convert XML Data to JSON using Python

[289 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"}}}
        


Need Programming or any Technical Related Books, Courses or Help Chat with us

Myntra [CPS] IN

Comments



Sponsored Advertisement


Udemy WW



Norton [CPS] WW

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


Search Tags