You don't dump a string but to a string.

d = {"foo": "bar"}
j = json.dumps(d)
print(j)

Output:

{"foo": "bar"}

It makes no sense to use a string as the argument of json.dumps which is expecting a dictionary or a list/tuple as the argument.

Serialize obj to a JSON formatted str using this conversion table.

Answer from user1907906 on Stack Overflow
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ json.html
json โ€” JSON encoder and decoder
2 weeks ago - In JSON, an object refers to any data wrapped in curly braces, similar to a Python dictionary. ... Be cautious when parsing JSON data from untrusted sources. A malicious JSON string may cause the decoder to consume considerable CPU and memory resources. Limiting the size of data to be parsed is recommended. This module exposes an API familiar to users of the standard library marshal and pickle modules. ... >>> import json >>> json.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}]) '["foo", {"bar": ["baz", null, 1.0, 2]}]' >>> print(json.dumps("\"foo\bar")) "\"foo\bar" >>> print(json.dumps('\u1234')) "\u1234" >>> print(json.dumps('\\')) "\\" >>> print(json.dumps({"c": 0, "b": 0, "a": 0}, sort_keys=True)) {"a": 0, "b": 0, "c": 0} >>> from io import StringIO >>> io = StringIO() >>> json.dump(['streaming API'], io) >>> io.getvalue() '["streaming API"]'
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ json-dumps-in-python
json.dumps() in Python - GeeksforGeeks
The json.dumps() function in Python converts a Python object (such as a dictionary or list) into a JSON-formatted string.
Published ย  January 13, 2026
Discussions

python - Using json.dumps(string) to dump a string - Stack Overflow
You don't dump a string but to a string. ... It makes no sense to use a string as the argument of json.dumps which is expecting a dictionary or a list/tuple as the argument. More on stackoverflow.com
๐ŸŒ stackoverflow.com
json.dump with python.dump
How could I do this but have the language be python when it dumps? You can't. I need to do this because json does not read tuples and other things I am wanting to save into the file. So change them into lists. More on reddit.com
๐ŸŒ r/learnpython
27
1
August 1, 2023
python json dumps - Stack Overflow
The u just indicates it's a unicode string in python < 3.0. Are you sure you want to convert to simple strings? ... You don't get rid of ", you're evaluating the string and returning a list. str_w_quotes is a terrible name for a list ... seems like you are passing my_str to json.dumps otherwise ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - How to prettyprint a JSON file? - Stack Overflow
For simple pretty-printing this also works without explicit parsing: print json.dumps(your_json_string, indent=4) 2014-08-04T14:07:01.31Z+00:00 More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
iO Flood
ioflood.com โ€บ blog โ€บ python-json-dumps
Python json.dumps() | Step-by-Step Guide
February 1, 2024 - In this example, we first define a Python dictionary named โ€˜dataโ€™. We then use the json.dumps() function to convert this dictionary into a JSON string, which we store in the variable โ€˜json_dataโ€™. Finally, we print โ€˜json_dataโ€™, which outputs the JSON string.
Find elsewhere
๐ŸŒ
DataCamp
datacamp.com โ€บ tutorial โ€บ json-data-python
Python JSON Data: A Guide With Examples | DataCamp
December 3, 2024 - In this example, we have a list ... string, which we store in the json_string variable. In Python, the json.dumps() function provides options for formatting and ordering the JSON output....
๐ŸŒ
Oreate AI
oreateai.com โ€บ blog โ€บ understanding-jsonloads-and-jsondumps-in-python-a-friendly-guide โ€บ 5e9af6929f92a705349cc4d4bfe74f75
Understanding json.loads and json.dumps in Python: A Friendly Guide - Oreate AI Blog
January 15, 2026 - It takes a Python object (like our newly created dictionary) and converts it back into a JSON-formatted string. This can be particularly useful when you're preparing data to send over an API or save to a file.
๐ŸŒ
Codecademy
codecademy.com โ€บ docs โ€บ python โ€บ json module โ€บ .dump()
Python | JSON Module | .dump() | Codecademy
May 29, 2025 - The .dump() function encodes a Python object as a JSON file. The process is known as serialization. The encoding conversion is based on this table.
๐ŸŒ
Anthropic
anthropic.com โ€บ engineering โ€บ advanced-tool-use
Introducing advanced tool use on the Claude Developer ...
Instead of each tool result returning to Claude, Claude writes a Python script that orchestrates the entire workflow. The script runs in the Code Execution tool (a sandboxed environment), pausing when it needs results from your tools. When you return tool results via the API, they're processed by the script rather than consumed by the model.
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ python โ€บ json_dumps_function.htm
Python json.dumps() Function
The Python json.dumps() function is used to serialize a Python object into a JSON-formatted string. This function is useful when working with APIs, logging structured data, or converting Python objects to JSON strings for storage or transmission.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-difference-between-json-dump-and-json-dumps
Difference between json.dump() and json.dumps() - Python - GeeksforGeeks
July 3, 2025 - It is a complete language-independent text format. To work with JSON data, Python has a built-in package called json. json.dumps() method can convert a Python object into a JSON string.
Top answer
1 of 15
3095

Use the indent= parameter of json.dump() or json.dumps() to specify how many spaces to indent by:

>>> import json
>>> your_json = '["foo", {"bar": ["baz", null, 1.0, 2]}]'
>>> parsed = json.loads(your_json)
>>> print(json.dumps(parsed, indent=4))
[
    "foo",
    {
        "bar": [
            "baz",
            null,
            1.0,
            2
        ]
    }
]

To parse a file, use json.load():

with open('filename.txt', 'r') as handle:
    parsed = json.load(handle)
2 of 15
500

You can do this on the command line:

python3 -m json.tool some.json

(as already mentioned in the commentaries to the question, thanks to @Kai Petzke for the python3 suggestion).

Actually python is not my favourite tool as far as json processing on the command line is concerned. For simple pretty printing is ok, but if you want to manipulate the json it can become overcomplicated. You'd soon need to write a separate script-file, you could end up with maps whose keys are u"some-key" (python unicode), which makes selecting fields more difficult and doesn't really go in the direction of pretty-printing.

You can also use jq:

jq . some.json

and you get colors as a bonus (and way easier extendability).

Addendum: There is some confusion in the comments about using jq to process large JSON files on the one hand, and having a very large jq program on the other. For pretty-printing a file consisting of a single large JSON entity, the practical limitation is RAM. For pretty-printing a 2GB file consisting of a single array of real-world data, the "maximum resident set size" required for pretty-printing was 5GB (whether using jq 1.5 or 1.6). Note also that jq can be used from within python after pip install jq.

๐ŸŒ
Note.nkmk.me
note.nkmk.me โ€บ home โ€บ python
Load, Parse, Serialize JSON Files and Strings in Python | note.nkmk.me
August 6, 2023 - You can use json.dumps() to convert Python objects, like dictionaries, into JSON-formatted strings.
๐ŸŒ
Proxy-Seller
proxy-seller.com โ€บ blog โ€บ how-to-work-with-json-dump-in-python-ultimate-guide
How to Use json.dump() in Python: Useful Guide
Learn how to use json.dumps in Python to convert and save data to JSON. Understand syntax, parameters, data types, and key differences between dump() vs dumps().
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ reading-and-writing-json-to-a-file-in-python
Reading and Writing JSON to a File in Python - GeeksforGeeks
The JSON package in Python has a function called json.dumps() that helps in converting a dictionary to a JSON object.
Published ย  August 5, 2025
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ python-pretty-print-json
How to Pretty Print JSON in Python | DigitalOcean
September 16, 2025 - Learn how to pretty print JSON in Python using built-in tools like json.dumps() and pprint to improve readability and debug structured data efficiently.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ json-dump-in-python
json.dump() in Python - GeeksforGeeks
January 13, 2026 - The json.dump() function in Python is used to convert (serialize) a Python object into JSON format and write it directly to a file.
๐ŸŒ
Analytics Vidhya
analyticsvidhya.com โ€บ home โ€บ python json.loads() and json.dump() methods
Python json.loads() and json.dump() methods - Analytics Vidhya
May 1, 2025 - Pythonโ€™s JSON.dump() function is used to serialize a Python object into a JSON-formatted string and write it to a file-like object. It inputs Python and file-like objects and writes the JSON data to the file.