Python with JSON Files

29/12/2020
With the growth and evolution of challenges in computer science, Python continues to rise as the primarily sought-after programming skill to solve data science problems.

What is JSON?

Among the tools, languages, and techniques commonly coupled with Python, JSON, or JavaScript Object Notation, is a file format that Python can read, write, and parse. JSON is commonly used for the exchange of data on the web. More specifically, JSON is the preferred text format when sending information from a web server to a browser or vice versa. This is advantageous simply because of its efficiency. JSON can be directly converted into JavaScript objects and thus interpreted, and JavaScript objects can be directly converted into JSON text. No additional hectic and disorganized parsing is required. When it comes to data exchange on the web, JSON is the go-to format one should use.

Python – Read, Write, and Parse JSON

Because JSON is language independent, Python objects can also be converted to JSON. Likewise, if Python receives data in JSON format, it can seamlessly convert it into Python objects. JSON is lightweight. You need only import JSON in Python to make use of Python’s reading JSON, writing JSON, and parsing JSON functionality. JSON is a built-in module in Python and you need only type import json at the top of your Python with JSON code to get started.

Why would one use JSON with Python? Well, if you are working on a web-based project which has to do with reading data, chances are you will have to stumble upon a Python-way out of the situation. Python is a language which can be used for so many different purposes and its growing functionality means that it is inevitable that you will encounter it in your programming journey. The support that Python gives to different dynamics of programming means that is a piece of cake to work with JSON in this language.

Basic JSON Module Functions in Python

The Format:

If you have used Python in a considerable amount in the past, then you must be familiar with a data structure known as the dictionary. In fact, it is quite probable that dictionaries have been your best friends when working in Python. Their easy-to-interpret format makes them the most sensible data structure to use when working with custom objects which require attributes of different types. And believe us when we say this that dictionaries are used insanely frequently in Python!

For example, if you want to use an Item object, you may create a dictionary like:

Now that you have seen what a dictionary looks like in Python code and when it is printed out, I’m sure its format makes a lot more sense now than when you’d first stumble upon that information enclosed in curly brackets.

Here’s where things start to get a little more relevant. Buckle up! Because we’re going to reveal the greatest intersection between Python and JSON. Remember the format of a dictionary in Python? That little code snippet with the braces shown above? Now observe the picture below:

Notice something? How can you not? It’s exactly the same! Python dictionaries and JSON text are in essentially the same format. You can probably deduce from just this what we are going to establish as the article progresses – that it is epically simple to “dump” Python dictionaries into JSON files. Dictionaries aren’t the only Python objects which can be converted into JSON text. Lists, tuples, booleans, strings, ints, and the list goes on are all interchangeable with JSON. One thing however which you might have failed to notice in the two pictures above is the quotation marks. JSON uses double quotation marks only, unlike Python. You might want to remember this detail to save yourself from an episode of “Where’s the damn bug this time??!”.

The Procedure:

The first thing you have to do is import Python’s built-in module for JSON.

Lets us first start by creating JSON from a Python dictionary. Writing JSON uses the method json.dumps(). This method returns a JSON string. The code screenshot below shows a dictionary converted to JSON and its output shown in Jupyter Notebook.

Now, let’s write this Python dictionary converted to JSON into a JSON file. The code snippet below shows this being done by using json.dump(). This will create a text file (txt extension file) with the JSON string written to it.

Here’s what the resulting file looks like:

Converting dictionaries into JSON creates JSON objects. But what about other data types in Python? What happens to them when you convert them to JSON text? This question doesn’t have a single answer. For example, lists and tuples are converted into the JavaScript equivalent Array. Ints and floats are converted into Number. Str is converted to String. Oh, and we almost forgot to mention! All these data types can be converted to JSON using the two methods json.dump() and json.dumps().

Now that you are familiar with the basic functionality of the dumps method, you can safely read up on how to better use its parameters. It has a parameter for data ordering called sort_keys. You can set the delimiter instead of using the default with the separators parameter. Indent can also be specified.

So here you have it! It is this easy to work with JSON files in Python! Use this guide and Python’s documentation to help you get started.

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Libvirt with Python

In one of my previous posts, I showed how one can get started with Libvirt and KVM. This virtualization stack is meant...
29/12/2020

Python Matplotlib Tutorial

In this lesson on Python Matplotlib library, we will look at various aspects of this data visualisation library which we...
29/12/2020

Install Python3 and IDLE on Ubuntu 18.04

Python 3.6 should be installed on Ubuntu 18.04 LTS by default. Python 3.7 (still in beta) is also available in the official...
28/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

Mua Proxy V6 Nuôi Facebook Spam Hiệu Quả Tại Onetcomvn
03/06/2024

Hướng dẫn cách sử dụng ProxyDroid để duyệt web ẩn danh
03/06/2024

Mua proxy Onet uy tín tại Onet.com.vn
03/06/2024

Thuê mua IPv4 giá rẻ, tốc độ nhanh, uy tín #1
28/05/2024

Thuê địa chỉ IPv4 IPv6 trọn gói ở đâu chất lượng, giá RẺ nhất?
27/05/2024