How to read JSON in Python

With the assistance of examples, you will learn how to parse, read, and write JSON files in Python. You will
also learn how to convert JSON to the dictionary and display it nicely.

JSON (JavaScript Object Notation) is a common data format for storing structured information. It is usual
to send and receive data in JSON format between a server and a web application. JSON is a data
transmission format that is widely use on the internet. All high-level programming languages may utilize
JSON (JavaScript Object Notation).

Importance and Use Cases of JSON

JSON is a data storage and representation format. Web development and configuration files are two of its
most prevalent applications. Let’s look into why:

  1. Online Development: JSON is extensively use in web applications to transport data from the server to the client and vice versa.
  2. JSON is also use to store configurations and settings in configuration files. To make a Google ChromeApp, for example, you must add a JSON file called manifest.
  3. JSON is use to provide the app’s name,description, current version, and other attributes and options.

The following are the primary properties of the JSON format:

  • There is a series of key-value pairs enclosed by curly brackets.
  • Using this format, each key is mapped to a specific value

Note: To make the data simpler to understand, we usually structure JSON with varying levels of indentation.
In this post, you will learn how to use Python to automatically create an indentation.

Data Types in JSON: Keys and Values

JSON files feature rules that specify which data types are acceptable for keys and values.

  • Strings must be use as keys.
  • A text, an integer, an array, a Boolean value (true/ false), null, or a JSON object can be used as values.

How do JSON and Python Dictionaries Interact?

When working with JSON files in Python, we cannot just read them and use the data in our program. This
is due to the fact that the whole file would be represented as a single string, making it impossible to access
the key-value pairs separately. unless, We utilize the JSON file’s key-value pairs to generate a Python dictionary that we can use in our application to read, use, and alter the data (if needed).

This is the most important link between JSON and Python Dictionaries. JSON is the data’s textual
representation and dictionaries are the data structures in memory that are produced when the application

Great. Now that you’ve learned more about JSON, let’s get into the nuts and bolts of working with JSON in

JSON is represented as a string in Python. As an example:


# JSON file format in Python
JSON_format = '{"name": "John", "languages": ["Python", "Java"]}'

How to use JSON with python?

This works by first having a JSON file on your hard drive.

The software then loads the file for processing, parses it, and finally makes it available for usage.

Import json Module

To work with JSON (string, or file containing JSON object), you can use Python’s JSON module. You need
to import the module before you can use it.


# For reading JSON file we import json module
import json

Parse JSON in Python

The JSON module makes it easy to parse JSON strings and files containing JSON objects.

Reading JSON file in Python

Create a file on your disk (name it: fruit.json). The python program below reads the JSON file and uses the
values directly.

The file can contain a one-liner. The file content of fruit.json is:

{ "fruit": "Apple", "size": "Large", "color": "Red"}

Save the file to fruit.json

Python Example: Then create the program below and run it.:

import json
# For reading json file
with open ('fruit.json', 'r') as myfile:
# Parsing json code
fruit_detail = json.loads(data)
# Output
print ("fruit name: " + str(fruit_detail['fruit']))
print ("fruit size: " + str(fruit_detail['size']))
print ("fruit color: " + str(fruit_detail['color']))



The above program will open the file fruit.json and parse it. You can access the JSON data like any

That’s all for this article if you have any confusion contact us through our website or email us at [email protected] or by using LinkedIn

Suggested Articles:

  1. How to Connect Python with Database
  2. How to create a virtual environment in Python?
  3. What is the difference between pip and pip3?

Leave a Comment