How to Convert XML to JSON in Python
Learn how to convert XML data to JSON format with a few lines of code.
It’s a little misleading to think of XML and JSON as competing data formats. After all, XML isn’t really a dedicated data interchange format at all - it’s a markup language (similar to HTML) with powerful data interchange capabilities. JSON, on the other hand, is a dedicated data interchange format, and it’s designed specifically for simple object storage.
Why Pick JSON over XML?
While XML is capable of storing more complex data relationships than JSON (involving all kinds of elements and external connections), that complexity usually amounts to overkill in most day-to-day situations. XML is also far less secure than JSON as a result of its complex markup features, and many applications & APIs only want to accept object data in JSON format as a result.
Thankfully, converting XML to JSON is a very straightforward task. We can use various open-source Python libraries for local scripts and applications (e.g., xmltodict
), and we can incorporate an XML to JSON conversion API into our applications for web server conversions.
Tutorial: Convert XML to JSON in Python
In this tutorial, we’ll learn how to call an XML to JSON web conversion API with Python code examples. We’ll end up with a quick and easy service that swaps out our XML content for JSON. All our conversions will occur in-memory to ensure the speed and security of our conversion.
Step 1: Install the SDK
To get started, we’ll run the following pip install command to incorporate the client SDK:
pip install cloudmersive-convert-api-client
Step 2: Add the Imports
Next up, we’ll add the following imports to the top of our controller:
from __future__ import print_function
import time
import cloudmersive_convert_api_client
from cloudmersive_convert_api_client.rest import ApiException
from pprint import pprint
Step 3: Configure a Cloudmersive API Key
In our configuration step, we’ll authorize our requests by supplying a Cloudmersive API key in the following snippet:
# Configure API key authorization: Apikey
configuration = cloudmersive_convert_api_client.Configuration()
configuration.api_key['Apikey'] = 'YOUR_API_KEY'
If we don’t have a Cloudmersive API key yet, we can fix that in a jiff! We just need to head to the Cloudmersive website and create a free account. This will give us a free API key with a limit of 800 API calls per month and zero commitments (our API call total will simply reset each month once we reach our limit).
Step 4: Instance the API Class and Call the Conversion Function
With our initial steps out of the way, we can get to the meat of our API call: creating an API instance and calling the core conversion function. We can handle both of those steps by copying the following snippet into our file:
# create an instance of the API class
api_instance = cloudmersive_convert_api_client.ConvertDataApi(cloudmersive_convert_api_client.ApiClient(configuration))
input_file = '/path/to/inputfile' # file | Input file to perform the operation on.
try:
# Convert XML to JSON conversion
api_response = api_instance.convert_data_xml_to_json(input_file)
pprint(api_response)
except ApiException as e:
print("Exception when calling ConvertDataApi->convert_data_xml_to_json: %s\n" % e)
We’ll make conversion by supplying our XML file path in the input_file
variable, and the API response will include a JSON object - simple as that!
Conclusion
In this tutorial, we learned how to call an API in our Python application that quickly and securely converts our XML data to JSON format.
Interested in more adding more data conversion APIs into your applications? Don’t forget to subscribe to this technical blog for more on that (and many other API solutions!).