Python read json file to dictionary

Introduction

In this code snippet, we are going to demonstrate how to read JSON data from file into a Python dictionary data structure.

String to JSON

The function json.loads can be used to load JSON data from string to dictionary. To load the data from file, we need to convert the file to string. Let us take an example…

Example JSON file

Assume we have the following json file test.json

Here is the Python code to load the JSON file above…

If you run the code snippet above, you should get the following output (using Python 2.x)…

We can also load the file without specifying a file handle. Take a look…

Loading the file in one shot

Thanks for reading. Questions ? please use the comments section below.

Tags:,
One Comment

Add a Comment

Your email address will not be published. Required fields are marked *