JSON Module

Last Updated: 22th August 2025


JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is often used to transfer data between a server and a web page.It is just like a dictionary.

List of Functions:

  • json.loads(): Converts a JSON string to a Python object.
import json
data = '{"name": "Amit", "age": 21, "course": "DS"}'
py_obj = json.loads(data)
print(py_obj)
print(type(py_obj))
  • json.dumps(py_object, indent, sort_keys): Converts a Python object to a JSON string.
import json
student = {"name": "Neha", "age": 22, "course": "AI"}
json_str = json.dumps(student,indent=4,sort_keys=True)
print(json_str)
  • json.load(file): Reads a JSON file and returns a Python object.
import json
with open("data.json", "r") as f:
    data = json.load(f)
    print(data)
  • json.dump(py_object, file, indent): Writes a Python object to a JSON file.
import json
student = {"name": "Sagar", "age": 22, "course": "AI"}
with open("data.json", "w") as f:
    json.dump(student, f, indent=4)