Python
JSON
json.dump
json.dumps
programming

What is the difference between json.dump and json.dumps in python?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

json.dump and json.dumps both serialize Python objects into JSON, but they return the result in different places. dump writes JSON to a file-like object, while dumps returns the JSON as a Python string.

json.dump: Write Directly to a File

Use json.dump(obj, fp) when you already have an open file and want the JSON written there.

python
1import json
2
3data = {"name": "Ada", "active": True}
4
5with open("user.json", "w", encoding="utf-8") as f:
6    json.dump(data, f, indent=2)

Nothing is returned that you normally use. The important effect is that the JSON text is written to user.json.

json.dumps: Return a String

Use json.dumps(obj) when you need the JSON in memory as a string.

python
1import json
2
3data = {"name": "Ada", "active": True}
4json_text = json.dumps(data, indent=2)
5print(json_text)

This is useful when you want to send JSON over HTTP, log it, embed it in another structure, or modify the resulting string before writing it somewhere.

The Names Make Sense Once You See the Pattern

Python uses a consistent naming pair:

  • 'dump writes to a stream or file'
  • 'dumps returns a string'
  • 'load reads from a stream or file'
  • 'loads reads from a string'

So the extra s in dumps means “string,” not “plural.”

Same Serialization Options

Both functions accept many of the same formatting and conversion options.

python
json.dumps(data, indent=2, sort_keys=True, ensure_ascii=False)

And similarly:

python
with open("user.json", "w", encoding="utf-8") as f:
    json.dump(data, f, indent=2, sort_keys=True, ensure_ascii=False)

That means the choice is not about formatting power. It is about whether you want a returned string or direct output to a file-like object.

A Practical Comparison

If you are saving configuration to disk, dump is the direct choice.

If you are building an HTTP response body, dumps is more natural.

python
payload = {"status": "ok"}
body = json.dumps(payload)

Then body can be passed to some other API. With dump, you would need a writable stream instead.

json.dump Can Still Write to Memory

Because dump writes to any file-like object, you can also pair it with io.StringIO when an API expects stream-style writing.

python
1import io
2import json
3
4buffer = io.StringIO()
5json.dump({\"x\": 1}, buffer)
6print(buffer.getvalue())

That does not change the conceptual difference. dump writes to a stream object. dumps returns the string directly.

The Read Side Uses the Same Pattern

The naming convention becomes even easier to remember when you look at deserialization:

  • 'json.load(f) reads JSON from a file-like object'
  • 'json.loads(text) reads JSON from a string'

Once you remember that pair, dump versus dumps is much less confusing.

That symmetry is the easiest way to remember the API without having to look it up every time. It also helps you choose the matching read API later.

Common Pitfalls

The most common mistake is calling json.dump(data) without supplying a file object and expecting a string back. That is what dumps is for.

Another issue is calling json.dumps and then writing the returned string to a file manually when json.dump would have been simpler.

A third pitfall is mixing up load and loads on the read side, which follows the same file-versus-string distinction.

Summary

  • 'json.dump writes JSON directly to a file-like object.'
  • 'json.dumps returns JSON as a Python string.'
  • The extra s means the function works with a string result.
  • Both support the same main formatting options.
  • Choose based on where you want the serialized JSON to go.

Course illustration
Course illustration

All Rights Reserved.