{"id":16433,"date":"2021-08-24T18:39:13","date_gmt":"2021-08-24T13:09:13","guid":{"rendered":"https:\/\/java2blog.com\/?p=16433"},"modified":"2021-08-30T18:35:16","modified_gmt":"2021-08-30T13:05:16","slug":"convert-xml-to-json-python","status":"publish","type":"post","link":"https:\/\/java2blog.com\/convert-xml-to-json-python\/","title":{"rendered":"Convert XML to JSON in python"},"content":{"rendered":"<p>XML (Extensible Markup Language) can create elements to store data. It is used for exchanging structured data over APIs and applications. JSON (JavaScript Object Notation) is also a very commonly used data structure notation to transfer the data between APIs and web applications. It is based on arrays and dictionaries. It was built as an improvement over XML.<\/p>\n<p>The Python programming language has modules available to parse both JSON and XML data. There is no direct method to convert XML to JSON so we will use a dictionary as an intermediary. We will learn how to perform this conversion in this article.<br \/>\n<div id=\"toc_container\" class=\"toc_light_blue no_bullets\"><p class=\"toc_title\">Table of Contents<\/p><ul class=\"toc_list\"><li><a href=\"#Using_the_xmltodict_and_json_module_to_convert_XML_data_to_JSON\">Using the xmltodict and json module to convert XML data to JSON<\/a><\/li><li><a href=\"#Using_the_xmletreeElementTree_and_json_module_to_convert_XML_data_to_JSON\">Using the xml.etree.ElementTree and json module to convert XML data to JSON<\/a><\/li><li><a href=\"#Using_the_xmljson_and_json_module_to_convert_XML_data_to_JSON\">Using the xmljson and json module to convert XML data to JSON<\/a><\/li><\/ul><\/div>\n\n<h2><span id=\"Using_the_xmltodict_and_json_module_to_convert_XML_data_to_JSON\">Using the <code>xmltodict<\/code> and <code>json<\/code> module to convert XML data to JSON<\/span><\/h2>\n<p>The <code>xmltodict<\/code> is a module used to read and parse XML data to dictionary and list type structures. We can use the <code>xmltodict.parse()<\/code> function from this module to achieve this. <\/p>\n<p>We can then use the <code>json<\/code> module to write this data to JSON type. The <code>json.dump()<\/code> function can write the given object to a JSON string. <\/p>\n<p>See the following code.<\/p>\n<pre code = \"python\">\nimport xmltodict, json\nxml_string = \"\"\"\n    <emp>\n      <id>102<\/id>\n      <name>Mark<\/name>\n      <dept>Accounts<\/dept>\n      <sal>75000<\/sal>\n    <\/emp>\n\"\"\"\nd = xmltodict.parse(xml_string)\njson_str = json.dumps(d)\nprint(json_str)    \n<\/pre>\n<p>Output:<\/p>\n<div class=\"content-box-green\">\n{&#8220;emp&#8221;: {&#8220;id&#8221;: &#8220;102&#8221;, &#8220;name&#8221;: &#8220;Mark&#8221;, &#8220;dept&#8221;: &#8220;Accounts&#8221;, &#8220;sal&#8221;: &#8220;75000&#8221;}}\n<\/div>\n<p>In the above example,<\/p>\n<ul>\n<li>We created an object which stores the XML string.<\/li>\n<li>The <code>xmltodict.parse()<\/code> function parses this to a dictionary type object.<\/li>\n<li>We pass this object to the <code>json.dump()<\/code> function which returns a JSON formatted string.<\/li>\n<\/ul>\n<h2><span id=\"Using_the_xmletreeElementTree_and_json_module_to_convert_XML_data_to_JSON\">Using the <code>xml.etree.ElementTree<\/code> and <code>json<\/code> module to convert XML data to JSON<\/span><\/h2>\n<p>The <code>xml.etree<\/code> module is an efficient method to parse the XML data as a tree. We can use this module to create a user-defined function that will parse our XML string to a dictionary, which we can write as a JSON file using the <code>json<\/code> module.<\/p>\n<p>See the code below.<\/p>\n<pre code = \"python\">\nfrom collections import defaultdict\nfrom xml.etree import ElementTree as ET\ndef xmldict(t):\n    d = {t.tag: {} if t.attrib else None}\n    children = list(t)\n    if children:\n        dd = defaultdict(list)\n        for dc in map(xmldict, children):\n            for k, v in dc.items():\n                dd[k].append(v)\n        d = {t.tag: {k: v[0] if len(v) == 1 else v\n                     for k, v in dd.items()}}\n    if t.text:\n        text = t.text.strip()\n        if children or t.attrib:\n            if text:\n              d[t.tag]['#text'] = text\n        else:\n            d[t.tag] = text\n    return d\nxml_string = \"\"\"\n    <emp>\n      <id>102<\/id>\n      <name>Mark<\/name>\n      <dept>Accounts<\/dept>\n      <sal>75000<\/sal>\n    <\/emp>\n\"\"\"\nxml_data = ET.XML(xml_string)\nd = xmldict(xml_data)\njson_str = json.dumps(d)\nprint(json_str)    \n<\/pre>\n<p>Output:<\/p>\n<div class=\"content-box-green\">\n{&#8220;emp&#8221;: {&#8220;id&#8221;: &#8220;102&#8221;, &#8220;name&#8221;: &#8220;Mark&#8221;, &#8220;dept&#8221;: &#8220;Accounts&#8221;, &#8220;sal&#8221;: &#8220;75000&#8221;}}\n<\/div>\n<p>Now, let us understand what we implemented in the above code.<\/p>\n<ul>\n<li>We pass the XML string using the <code>ET.XML()<\/code> function.<\/li>\n<li>We pass this data to a function called <code>xmldict()<\/code>.<\/li>\n<li>We process this data to return a dictionary.<\/li>\n<li>This dictionary is then passed to the <code>json.dump()<\/code> function, which returns the final JSON data.<\/li>\n<\/ul>\n<h2><span id=\"Using_the_xmljson_and_json_module_to_convert_XML_data_to_JSON\">Using the <code>xmljson<\/code> and <code>json<\/code> module to convert XML data to JSON<\/span><\/h2>\n<p>The <code>xmljson<\/code> is a new and simple library available to process XML data in Python. It provides different objects to parse the data differently. <\/p>\n<p>We can use the <code>xmljson.Yahoo()<\/code> constructor to initialize an object that can be used to work with XML string. The <code>xtree.ElementTree.fromstring()<\/code> function returns an XML string, and we use the <code>data()<\/code> function with this object to parse this string. This will return a dictionary with the required data.<\/p>\n<p>Then as we did earlier, we convert this dictionary to JSON using the <code>json.dump()<\/code> function.<\/p>\n<p>For example,<\/p>\n<pre code = \"python\">\nimport xmljson\nfrom xml.etree import ElementTree as ET\nxml_string = \"\"\"\n    <emp>\n      <id>102<\/id>\n      <name>Mark<\/name>\n      <dept>Accounts<\/dept>\n      <sal>75000<\/sal>\n    <\/emp>\n\"\"\"\nP = xmljson.Yahoo(dict_type = dict)\nd = P.data(ET.fromstring(xml_string))\njson_str = json.dumps(d)\nprint(json_str)    \n<\/pre>\n<p>Output:<\/p>\n<div class=\"content-box-green\">\n{&#8220;emp&#8221;: {&#8220;id&#8221;: &#8220;102&#8221;, &#8220;name&#8221;: &#8220;Mark&#8221;, &#8220;dept&#8221;: &#8220;Accounts&#8221;, &#8220;sal&#8221;: &#8220;75000&#8221;}}\n<\/div>\n<p>That&#8217;s all about how to convert XML to JSON in Python.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Table of ContentsUsing the xmltodict and json module to convert XML data to JSONUsing the xml.etree.ElementTree and json module to convert XML data to JSONUsing the xmljson and json module to convert XML data to JSON XML (Extensible Markup Language) can create elements to store data. It is used for exchanging structured data over APIs [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":16514,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_mi_skip_tracking":false},"categories":[145],"tags":[],"_links":{"self":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/16433"}],"collection":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/comments?post=16433"}],"version-history":[{"count":0,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/16433\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/media\/16514"}],"wp:attachment":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/media?parent=16433"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/categories?post=16433"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/tags?post=16433"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}