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.
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.
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:
- '
dumpwrites to a stream or file' - '
dumpsreturns a string' - '
loadreads from a stream or file' - '
loadsreads 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.
And similarly:
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.
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.
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.dumpwrites JSON directly to a file-like object.' - '
json.dumpsreturns JSON as a Python string.' - The extra
smeans 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.

