In this python tutorial, I’ll walk you through how to create a script that can convert CSV, SQL or Google Sheets data into JSON or XML. The main modules of this tutorial are the JSON and CSV.
Python Modules: JSON, CSV
- Why JSON, XML converter Matters
- JSON Converter
- XML Converter
- Full Python Scripts of JSON and XML converter
Why JSON, XML converter Matters
This data type is widely being used in data interchange and applications such as APIs, mobile application, web applications, and IoT etc. Basically, you might be aware, most automation and bot tutorials I shared before are more or less dealing with JSON format data. And if you are a marketer, JSON product data feed must be a familiar term you often hear.
XML, or Extensible Markup Language, is a markup language similar to HTML, but it doesn’t have predefined tags to use. Instead, you define your own tags designed specifically for your needs. This is a powerful way to store data in a format that can be stored, searched, and shared.
XML is an alternative data format to JSON. If you are a SEO expert, XML must be a familiar term to you because Google SEO sitemap is in a XML data format. Some platforms like WeChat API allows developers to use either JSON or XML data format to interchange with servers, or applications.
A JSON converter or XML converter embedded into your application is crucial. It’s because it can create a seamless communication with different servers or platforms. For example, probably your data inventory is stored in SQL format, however your buyers might pay you and grab your data via an API. So these two data format is super helpful on the interchange and communication.
In this tutorial, I’ll take a CSV file for example and if you are interested in the other two data formats SQL and Google Sheets, please check out this article.
First thing first, we need to create an empty dictionary data format as it’s essentially a mix of dictionaries and lists. In python, there are several core data types. They are integer, string, float, boolean, list, Tuple and Dictionary. I’ll release another video to elaborate more in details.
Then, you can open to output your prepared data in a CSV format using the DictReader method. When we loop each row of data in the CSV we need to set a key at least it’s because JSON is a structure like a tree. There are branches under a root and lots of small branches under each branch. We need to select at least one key as the entry point for different type of data.
Last but not least, we create a json file using the open method and dumps the data from CSV with an indent 4. The indent parameter specifies the spaces that are used at the beginning of a line. We can use the indent parameter of json.dump() to specify the indentation value. By default, when you write JSON data into a file, Python doesn’t use indentations and writes all data on a single line, which is not readable.
Although XML data format doesn’t have any predefined tags needed to use, it has required a few basic header and footer settings.
First thing first, we need to claim the version and the encoding version
<?xml version=”1.0” encoding=”UTF-8”?>
Then, it’s the XML file body naming
Within the body, it’s the data from a CSV file. It consists of the key name and each line of data name and value.
In the coding, we would use the format string and write method to convert CSV into the XML data.
Full Python Scripts of JSON and XML converter
If you are interested in the full Python script of Python Tutorial 48: JSON, XML Converters to CSV, SQL, Google Sheets Data into JSON, XML, please subscribe to our newsletter by adding the message “Python Tutorial 46”. We would send you the script immediately to your mailbox.
I hope you enjoy reading Python Tutorial 48: JSON, XML Converters to CSV, SQL, Google Sheets Data into JSON, XML. If you did, please support us by doing one of the things listed below, because it always helps out our channel.
- Support my channel through PayPal (paypal.me/Easy2digital)
- Subscribe to my channel and turn on the notification bell Easy2Digital Youtube channel.
- Follow and like my page Easy2Digital Facebook page
- Share the article to your social network with the hashtag #easy2digital
- Buy products with Easy2Digital 10% OFF Discount code (Easy2DigitalNewBuyers2021)
- You sign up for our weekly newsletter to receive Easy2Digital latest articles, videos, and discount code on Buyfromlo products and digital software
- Subscribe to our monthly membership through Patreon to enjoy exclusive benefits (www.patreon.com/louisludigital)