This article will explain how to handle JSON files and data in Python programming language. Python comes with a built-in “json” module that makes handling JSON data easy and straightforward.

About JSON

JSON (JavaScript Object Notation) is a file format and data storage standard that uses key-value pairs to store and exchange data. It is the most widely used data exchange format, often seen in RESTful APIs, lightweight databases, config files, and other offline or online software that needs to store, retrieve and exchange data. Most programming languages include libraries to parse and write JSON data by default and JSON is a programming language agnostic data format. Values stored in a JSON file or payload usually contain strings, numbers and serializable data types like lists (arrays).

JSON and Python Dictionaries

JSON data in python is converted into a dictionary object by using “load” method. It is often seen that some people equate json data and a python dictionary, as the syntax to define both of them is nearly the same. However, json data is nothing but a text string structured in a rigid syntax while a python dictionary is a data structure object stored in memory. If you want to store dictionary data in a text file or send it to another non-python program, you will have to first convert it into a text string (bytes). This dumped / converted text string is defined in JSON’s standard syntax and the process of converting a python dictionary into a json compatible string is called serialization.

Reading and Dumping JSON Data in Python

JSON data can be retrieved as a response form a server, read from file, sliced from URL query parameters and so on. This article will mainly focus on reading json data from a locally stored file. Let’s assume that a “test.json” file contains following data:

{“codename”: “Eoan Ermine”, “version”: “Ubuntu 19.10”}

To read the test.json file in python, you can use the code below:

import json

with open (“test.json”) as f:


  data = json.load(f)

dump = json.dumps(data)

print (data)

print (type(data))

print (dump)

print (type(dump))

The first line in the above code imports json module. In the next line, “with open” is used to safely read the file contents. Within the “with open” block, “json.load” method is used to read and store file contents in the “data” variable. Data loaded in the previous step is converted back into json string using the “json.dump” method. Running the code above will show the following output:

{‘codename’: ‘Eoan Ermine’, ‘version’: ‘Ubuntu 19.10’}

<class ‘dict’>

{“codename”: “Eoan Ermine”, “version”: “Ubuntu 19.10”}

<class ‘str’>

Notice in the output above that the “json.load” method reads raw json data into a python dictionary while “json.dumps” methods converts a dictionary into a string that is compatible with JSON structure. Once a JSON object has been converted into a python dictionary object, you can use built-in python dictionary methods to handle the data. The example above is pretty basic and doesn’t include arrays in JSON data or nested values. However within python, you can handle these values like any other nested dictionaries and lists.

Sorting and Pretty Printing JSON Data

The “json.dump” method supports some optional parameters for sorting keys and pretty printing the output for improved readability.

import json

with open (“test.json”) as f:


  data = json.load(f)

dump = json.dumps(data, sort_keys=True, indent=4)

print (dump)

The code above will show the following output:

{


  “codename”: “Eoan Ermine”,


  “version”: “Ubuntu 19.10”


}

Data Type Conversions

The list below illustrates how JSON values are converted into python objects and vice versa.

JSON Python
string str
number int or float
true, false True, False
null None
array list
object dict

Json.tool Command Line Module

Python includes a nice command line utility “json.tool” that can be used to validate and pretty print JSON strings and files.

$ echo ‘{“codename”: “Eoan Ermine”, “version”: “Ubuntu 19.10”}’ | python3 -m json.tool

Running the command above will show you following output:

{


  “codename”: “Eoan Ermine”,


  “version”: “Ubuntu 19.10”


}

You can also use json.tool with JSON files. Replace “in_file” and “out_file” in the command below with your desired values:

$ python3 -m json.tool in_file out_file

Other than pretty printing, sorting and validating JSON data, json.tool doesn’t do anything else. So if you want to manipulate any JSON data, you have to write your own custom code using the built-in JSON module.

About the author

How to Read and Write Json Files in Python JSON Python

Nitesh Kumar

I am a freelancer software developer and content writer who loves Linux, open source software and the free software community. I maintain a blog that lists new Android deals everyday.