
In this article, we have discussed different ways to convert a JSON string or File to XML in Python. Xmltodict.unparse(python_dict,output=xml_file)Īfter executing the above code, the content of the JSON file "person.json" will be saved as XML in the "person.xml" file. You can observe this in the following example.

Then, we will save the python dictionary to the XML file using the unparse() method defined in the xmltodict module.


Xml_string=xmltodict.unparse(python_dict) JSON FileĪll the steps discussed above have been implemented in the following example. We will convert the following JSON file to XML. Once we get the python dictionary, we will convert it to an XML string using the unparse() method from the xmltodict module.

The load() method takes the file pointer to the JSON file as its input argument and returns a python dictionary. After execution, the open() function returns a file pointer.Īfter this, we will load the json file into a python dictionary using the load() method defined in the json module. The open() function takes the file name as its first input argument and the python literal “r” as its second input argument. To convert a JSON file to an XML string, we will first open the JSON file in read mode using the open() function. XML File Convert JSON File to XML String in Python Xmltodict.unparse(python_dict,output=file) Here is an example of a simple JSON object:
