Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Python · Module

json

Python’s json module encodes compatible Python data as JSON and decodes JSON back into Python objects. It supports strings, file-like objects, formatting options, hooks, and a command-line interface.

Since
3.14

How it works

The module converts compatible Python object hierarchies to JSON text and reconstructs Python objects from JSON text. Use dumps and loads for strings, or dump and load with file-like objects; optional hooks and formatting arguments customize conversion.

Syntax

json.dump(obj, fp, *, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, sort_keys=False, **kw)
json.dumps(obj, *, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, sort_keys=False, **kw)
json.load(fp, *, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw)
json.loads(s, *, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw)

Parameters

NameTypeDescription
objobjectPython object to serialize, for dump or dumps.
fptext file-like objectFile-like destination with a write method, for dump.
fptext or binary file-like objectFile-like source with a read method, for load.
keyword optionskeyword argumentsOptional formatting, conversion, ordering, and parsing controls such as indent, sort_keys, hooks, and parse functions.

Returns

dumps returns a JSON-formatted string; loads and load return the corresponding Python object; dump writes JSON to the supplied file-like object.

Key points

  • The module offers both string-based and file-based encoding and decoding functions with a consistent standard-library-style API.
  • json.dumps converts a Python value into JSON text and returns that text.
  • Running python -m json provides a command-line way to validate and format JSON input.
  • The module implements encoding and decoding for the JSON data-interchange format.

Common mistakes

  • JSON is a data-interchange format rather than a general-purpose Python object serialization format; values must fit the module's conversion rules.
  • Default output formatting is not necessarily compact or sorted; pass indent, separators, or sort_keys when a particular representation is required.
  • The command-line interface is available in modern Python, but invoking it requires valid input when using it to validate or pretty-print JSON.

Example: Serialize and deserialize JSON

import json

payload = {"name": "Ada", "roles": ["admin", "editor"]}
text = json.dumps(payload, indent=2, sort_keys=True)
print(text)

restored = json.loads(text)
assert restored == payload

Serialize a Python object to formatted JSON text, then parse that text back into a Python object.

When to use it

Exchange structured application data

An HTTP client or server needs a portable payload representation.

Store JSON in files

An application needs to persist simple configuration or state.

Check JSON from the command line

A developer needs to inspect or validate JSON outside application code.

Interview questions

QWhich functions serialize and deserialize strings versus files?

Use json.dumps or json.dump to serialize Python data, and json.loads or json.load to deserialize it. The string variants operate on text, while the file variants use file-like objects.

Follow-up: How would you format deterministic, readable output? Pass sort_keys=True for key ordering and indent for human-readable indentation.

QWhat is the difference between dump/dumps and load/loads?

json.dumps returns a JSON-formatted string, whereas json.dump writes JSON to a file-like object. Conversely, json.loads parses a string and json.load reads from a file-like object.

Follow-up: What should be used when processing a stream incrementally? The documented load API expects a file-like object containing a JSON document; streaming protocol design requires handling framing separately.

QHow can Python's json module be used from the command line?

The module provides a command-line interface through python -m json for validating and pretty-printing JSON objects. This is useful for checking or displaying JSON outside application code.

Follow-up: How can output be made easier to inspect? Use the command-line interface or supply indent when calling dumps or dump.

  • json.JSONEncoder
  • json.JSONDecoder
  • json.dump
  • json.dumps
  • json.load
  • json.loads
  • json.tool

Source: json — JSON encoder and decoder — Python 3.15.0 documentation. Checked 2026-10-15.