Reading and Writing JSON to a File in Python (original) (raw)

The full form of **JSON is Javascript Object Notation.

Reading JSON from a file using Python

Reading JSON in Python means retrieving JSON data from a file or string and converting it into Python objects like dictionaries or lists. This process is called deserialization. Python offers two main ways to read JSON data:

1. Using json.load()

The JSON package has **json.load() function that loads the JSON content from a JSON file into a dictionary. It takes one parameter:

**File pointer: A file pointer that points to a JSON file.

**Example: Read a JSON file and convert its contents to a Python dictionary.

Python `

import json

Read from file and parse JSON

with open("sample.json", "r") as f: data = json.load(f)

print(data) print(type(data))

`

**Output:

Output_usingjsonload

Output using json.load()

**Explanation: We open sample.json in read mode and use **json.load() to parse its content into a Python dictionary, which we then print along with its data type.

2. Using json.loads()

**json.loads() function is used to parse a JSON string and convert it into a Python object. It takes one parameter:

**JSON string: A valid JSON-formatted string.

**Example: Convert a JSON-formatted string into a Python dictionary.

Python `

import json json_str = '{"name": "Francis", "age": 25, "city": "New York"}' data = json.loads(json_str)

print(data) print(type(data))

`

Output_usingjsonloads

Output using json.loads()

**Explanation: We define a JSON string and use json.loads() to parse it into a Python dictionary, then print the resulting object and its type.

Writing JSON to a file in Python

Writing data to a JSON file in Python involves converting Python objects like dictionaries into JSON format and saving them to a file. This process is called serialization. Let's explore two methods to write a JSON file in Python.

1. Using json.dumps()

The JSON package in Python has a function called json.dumps() that helps in converting a dictionary to a JSON object. It takes two parameters:

After converting the dictionary to a JSON object, simply write it to a file using the "write" function.

**Example: Convert a dictionary to a JSON string and write it to a file.

Python `

import json data = { "name": "sathiyajith", "rollno": 56, "cgpa": 8.6, "phone": "9976770500" }

json_str = json.dumps(data, indent=4) with open("sample.json", "w") as f: f.write(json_str)

`

**Output:

Output_usingjsondumps

Output using json.dumps()

**Explanation: We create a dictionary, convert it to a JSON-formatted string using **json.dumps() with 4-space indentation and write it to sample.json using the **write() method.

2. Using json.dump()

Another way of writing JSON to a file is by using json.dump() method. The JSON package has "dump" function which directly writes the dictionary to a file in the form of JSON, without needing to convert it into an actual JSON object. It takes 2 parameters:

**Example: Directly write a dictionary to a JSON file without converting it first.

Python `

import json data = { "name": "sathiyajith", "rollno": 56, "cgpa": 8.6, "phone": "9976770500" }

with open("sample.json", "w") as f: json.dump(data, f)

`

**Output

Output_usingjsondump

Output using json.dump()

**Explanation: We create a dictionary and use json.dump() to directly write it to **sample.json, skipping the need to manually convert it to a JSON string.

Python vs JSON Object Mapping

PYTHON OBJECT JSON OBJECT
Dict object
list, tuple array
str string
int, long, float numbers
True true
False false
None null